C++从变量中获取变量

问题描述:

请问有其他解决方案吗?C++从变量中获取变量

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

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

+0

是这些指针或字符串? – 2014-12-13 15:07:14

+0

只有字符串... – skyndas 2014-12-13 15:08:12

+1

质量问题和标题很差。你能解释一下你想完成什么吗? – ezaquarii 2014-12-13 15:11:25

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

if(!http_piconpath) http_piconpath = http_tpl; 

或者

http_piconpath = http_piconpath ? http_piconpath : http_tpl; 

如果皮孔为null,它得到第三方物流的价值;如果两者都为空,则没有任何变化。

你提供有关你在做什么的信息非常少。假设你使用了你的注释中的字符串,你得到的if语句对字符串无效,你会看到你的编译器尖叫着它不能将字符串转换为bool。以下是一个非常基本的例子。请注意,您必须初始化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; 
}