2014-12-13 133 views
-6

請問有其他解決方案嗎?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

回答

0

假設都是(兼容類型)的指針,

if(!http_piconpath) http_piconpath = http_tpl; 

或者

http_piconpath = http_piconpath ? http_piconpath : http_tpl; 

如果皮孔爲null,它得到第三方物流的價值;如果兩者都爲空,則沒有任何變化。

1

你提供有關你在做什麼的信息非常少。假設你使用了你的註釋中的字符串,你得到的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; 
}