C++从变量中获取变量

C++ get variable from variable

本文关键字:变量 获取 C++      更新时间:2023-10-16

请问还有其他解决方案吗?

if(!http_piconpath && http_tpl)
            { http_piconpath = http_tpl; }

如果不存在http_piconpath但存在http_tpl则将值从 http_tpl 分配给 http_piconpath

你提供的关于你正在做什么的信息很少。假设你使用注释中的字符串,你得到的 if 语句对字符串无效,你会看到你的编译器尖叫它无法将字符串转换为布尔值。下面是一个非常基本的例子。请注意,您必须初始化http_piconpath,否则它将具有垃圾值,您将不知道其值是否已设置。

#include <iostream>
#include <string>
using namespace std;
int main()
{
   string http_piconpath = "";
   string http_tpl = "string";
   if(http_piconpath == "" && http_tpl != "") {
       http_piconpath = http_tpl;
   }
   cout << http_piconpath << endl;
   return 0;
}

假设两者都是指针(兼容类型),

if(!http_piconpath) http_piconpath = http_tpl;

http_piconpath = http_piconpath ? http_piconpath : http_tpl;

如果 picon 为 null,则获取 tpl 的值;如果两者都为 null,则不会更改任何内容。