如果我在构造函数中写 return 语句怎么办?
如果我在构造函数中写 return 语句怎么办?是否符合标准?
struct A
{
A() { return; }
};
上面的代码编译良好,在 ideone 上没有任何错误。但以下代码却没有:
struct A
{
A() { return 100; }
};
它在 ideone 处给出了此错误:
错误:从构造函数返回值
我知道从构造函数返回值根本没有意义,因为它没有明确提及返回类型,而且我们毕竟无法存储返回值。但我很想知道:
- C++ 标准中的哪条语句允许第一个示例但禁止第二个示例?有没有明确的声明?
- 第一个示例中的返回类型是
void
吗? - 是否有任何隐式返回类型?
What if I write return statement in constructor? Is it standard conformant?
struct A
{
A() { return; }
};
The above code compiles fine, without any error at ideone. But the following code doesn't:
struct A
{
A() { return 100; }
};
It gives this error at ideone:
error: returning a value from a constructor
I understand that returning value from constructor doesn't make sense at all, because it doesn't explicitly mention return type, and we cannot store the returned value after all. But I'm curious to know :
- Which statement from the C++ Standard allows the first example but forbids the second one? Is there any explicit statement?
- Is the return type in the first example
void
? - Is there any implicit return type at all?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
是的,在构造函数中使用 return 语句是完全标准的。
构造函数是不返回值的函数。不返回值的函数系列包括:void 函数、构造函数和析构函数。 C++标准中的6.6.3/2中有规定。同样的 6.6.3/2 规定,在不返回值的函数中使用带有参数的
return
是非法的。此外,12.1/12 规定
请注意,顺便说一句,在 C++ 中,在 void 函数中使用带有参数的
return
是合法的,只要return
的参数具有void
类型。 code>但这在构造函数中是不允许的,因为构造函数不是 void 函数。
还有一个与构造函数中使用
return
相关的相对模糊的限制:在构造函数的函数 try 块中使用return
是非法的(与其他函数一起使用)好的)Yes, using return statements in constructors is perfectly standard.
Constructors are functions that do not return a value. The family of functions that do not return a value consists of: void functions, constructors and destructors. It is stated in 6.6.3/2 in the C++ standard. The very same 6.6.3/2 states that it is illegal to use
return
with an argument in a function that does not return a value.Additionally, 12.1/12 states that
Note, BTW, that in C++ it is legal to use
return
with an argument in a void function, as long as the argument ofreturn
has typevoid
This is not allowed in constructors though, since constructors are not void functions.
There's also one relatively obscure restriction relevant to the usage of
return
with constructors: it is illegal to usereturn
in function-try-block of a constructor (with other functions it is OK)也许在构造函数中具有无类型返回的概念是为了控制构造函数的终止。
Perhaps the notion of having typeless return in constructors is to control the termination of constructor function.
根据定义,构造函数确实返回一些东西。
对于下面的代码,
这里我们创建一个 void 类型函数来初始化我们的狗对象,然后在我们的 main 函数中,我们必须在新的狗对象上调用该 init 函数。但是如果使用构造函数,我们的代码看起来就像这样
这段代码本质上是同样的事情,但它是为了表明构造函数初始化传入的变量并返回对象本身。
你不能说
A constructor by definition does return something.
For the code below,
Here we create a void type function that initializes our dog object, then in our main function we must call that init function on our new dog object.But if use a constructor our code looks instead like
This code does essentially the same thing but it is to show that a constructor initializes the passed in variables and returns the object itself.
You cannot say