将值分配给同一行上的多个声明 [英] Assigning values to multiple declarations on same line

查看:159
本文介绍了将值分配给同一行上的多个声明的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建变量

std::string str1,str2,str3,str4 = "Default";

但是变量没有Default值。如何为这种创建的变​​量赋值

however the variables do not have the Default value. How can I assign values to variables created this way

推荐答案

str4 有你想要的价值。你只是没有初始化其他人。使用:

str4 will have the value you're looking for. You just didn't initialize the others. Use:

std::string str1 = "Default", str2 = "Default", str3 = "Default", str4 = "Default";

或者,可能更好:

std::string str1 = "Default";
std::string str2 = "Default";
std::string str3 = "Default";
std::string str4 = "Default";

如果你担心这么多打字,你可以使用赋值而不是初始化: p>

If you're concerned about doing so much typing, you can use assignment instead of initialization:

std::string str1, str2, str3, str4;
str1 = str2 = str3 = str4 = "Default";

但是这有不同的语义,是(IMHO)有点蠢。

But that's got different semantics and is (IMHO) a bit hinky.

这篇关于将值分配给同一行上的多个声明的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆