当我试图用这样的单个参数构造一个对象时,我的一个同事发现了这个:
char testString[] = "test string";
...
MyClass instance(QString(testString));
Run Code Online (Sandbox Code Playgroud)
他们意识到如果你用一个参数构造函数式习语,它将被解析为一个前向声明的函数,例如
char test[] = "hello";
MyClass instance1(0, test) //constructs an object
MyClass instance2(test) //invalidly forward-declares a function
Run Code Online (Sandbox Code Playgroud)
但他们被他们认为应该工作的东西所困扰,因为它在括号内包含一个函数式转换或单参数构造函数调用.
我已经发现他们的构造函数调用被解析为:
MyClass instance(QString testString);
Run Code Online (Sandbox Code Playgroud)
它不是将变量testString传递给强制转换运算符,而是声明一个具有相同名称参数的函数.我的问题是,它为什么这样做?它似乎允许参数的名称被括号括起来.
这是我的一位同事发现的
不,它在几十年前被"发现",是一种非常着名的语言怪癖.
我的问题是,它为什么这样做?它似乎允许参数的名称被括号括起来.
它是.C声明语法不是那么规定,不允许这种特定情况.在一般情况下,它对于更复杂的结构非常有用:
void foo(int (*func)(char));
// (takes a pointer to a function returning int and taking char, bool)
// as opposed to:
void foo(int *func(char));
// (takes a function returning int* and taking char)
// as this is literally equivalent to the clearer:
void foo(int* func(char));
Run Code Online (Sandbox Code Playgroud)
并且,在C++中:
void foo(int (&array)[5]) {}
// (takes a reference to an array of five integers)
// as opposed to:
void foo(int &array[5]) {}
// (takes an array of five `int&`s ... and fails to compile accordingly)
Run Code Online (Sandbox Code Playgroud)
对于您的具体案例,这是一个公认的笨拙解决方案:
MyClass instance((QString(testString)));
// ^ ^
Run Code Online (Sandbox Code Playgroud)
或者,自C++ 11以来更有帮助:
MyClass instance(QString{testString});
// ^ ^
Run Code Online (Sandbox Code Playgroud)
这实际上是引入统一初始化语法的驱动因素之一.
如果你用一个参数构造函数式习语,它将被解析为一个前向声明的函数,例如
Run Code Online (Sandbox Code Playgroud)MyClass instance2("hello") //invalidly forward-declares a function
这种说法完全是不真实的.
此外,char *testString = "test string";已被弃用17年,非法4年.
| 归档时间: |
|
| 查看次数: |
170 次 |
| 最近记录: |