如何在C ++中有条件地设置C样式字符串?

时间:2017-12-12 14:31:32

标签: c++ initialization switch-statement

我正在使用C ++而不想使用SELECT* FROM (SELECT @rowsnum := @rowsnum + 1 AS ae1num, Concat(ae1.a, ae1.b) AS id, ae1.* FROM A as ae1, (SELECT @rowsnum := 0) r ORDER BY ae1.a, ae1.b) ae1 LEFT JOIN (SELECT aenum, Concat(ae111.a, ae111.b) AS id, aecm.* FROM (SELECT @rownum := @rownum + 1 AS aenum, Concat(ae11.a, ae11.b) AS id, ae11.* FROM A as ae11, (SELECT @rownum := 0) a ORDER BY ae11.a, ae11.b) ae111 LEFT JOIN B as aecm ON ae111.a = aecm.a AND ae111.b = aecm.b ORDER BY ae111.a, ae111.b) aec ON ae1.a = aec.a AND ae1.b = aec.b AND aec.aenum = ae1.ae1num 。我试图根据条件的值设置C样式字符串的值。这可能吗?

以下是最低工作示例:

std::string

此程序没有错误,但将非#include <iostream> void print_thing(int a) { char* message; switch (a) { case 1: message = "case 1"; break; default: message = "case 2"; break; } std::cout << message << std::endl; } int main() { int a{ 5 }; print_thing(a); } const设置为字符串文字似乎不好。有更好的方法吗?

1 个答案:

答案 0 :(得分:4)

代码很好,除了C ++中的字符串文字类型为const char[](与C char[]不同)。因此,您的变量必须声明为const char* message;