C++从变量中获取变量
C++ get variable from variable
请问还有其他解决方案吗?
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,则不会更改任何内容。
相关文章:
- 尝试通过OCI例程从Oracle获取blob数据,但出现错误:ORA-01008:并非所有变量都绑定
- 使用递归时获取变量的奇怪值
- 从 C 中的变量获取文件的路径,C++
- 我可以从其变量中获取对类对象的引用吗?
- 为什么在分配给成员变量之前获取unique_ptr的返回是一个问题?
- std::cin 从控制台获取两种不同的变量类型,'storing'以后使用第二种类型?
- 如何使用可变参数模板获取参数的变量号及其大小C++
- 获取具有静态局部变量的绑定/推断捕获 lambda 的函数指针
- constexpr 函数在编译时获取值,即使我的变量不是 constexpr
- 如何在不为其声明变量的情况下获取和使用常量值的地址?
- 附加调试器并以编程方式获取变量地址 Visual Studio
- 是否可以将变体的索引作为 constexpr 变量获取?
- 为什么我无法获取 MSVS2019 / C++ 中字符或uint8_t变量的内存地址?
- 我正在尝试获取类私有成员中变量的输入
- C++/Win32 构造函数不使用从对话框获取的字符串初始化变量
- 使用基地址和偏移量获取变量的地址
- C++11 - 获取编译时的所有类变量,并在没有 Boost 的情况下为它们调用方法
- 使用 WindowSetup 类变量获取错误
- fstream 变量获取文本文件中的所有行
- 从类型而不是从变量获取指针级别