By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,233 Members | 1,939 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,233 IT Pros & Developers. It's quick & easy.

return statement in C++ constructor

P: n/a
Sorry, some people may have asked this question before. It is really hard to
find relevant articles about this topic on the web using key words.

I know it is not recommended to use return statement in constructor. But,
1. How about a return statement without any return value? Does the execution
of the constructor stop at the return statement?

2. How does the compiler handle the return statement with a value? Does it
just give a warning?

Thanks. A. Y.


Jul 22 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
On Tue, 16 Dec 2003 14:38:54 -0600 in comp.lang.c++, "Test"
<if******@mailnew.com> was alleged to have written:
I know it is not recommended to use return statement in constructor. But,
1. How about a return statement without any return value? Does the execution
of the constructor stop at the return statement?
The constructor returns. Execution continues at the point where the
object is being constructed.
2. How does the compiler handle the return statement with a value? Does it
just give a warning?


The program is ill-formed and the compiler is required to issue a
diagnostic message. Whether the message is regarded as a warning or an
error is between you and your compiler vendor, as is whether or not the
compiler will continue on in some fashion after the message.

Jul 22 '05 #2

P: n/a

"Test" <if******@mailnew.com> wrote in message
news:br**********@newshost.mot.com...
Sorry, some people may have asked this question before. It is really hard to find relevant articles about this topic on the web using key words.

I know it is not recommended to use return statement in constructor. But,
1. How about a return statement without any return value? Does the execution of the constructor stop at the return statement?
Yes, I think that would be fine.
2. How does the compiler handle the return statement with a value? Does it
just give a warning?


That really depends on your compiler - I don't know what the standard says,
but I doubt a compiler should allow something like that. It doesn't make
sense. Don't you have a compiler?
Jul 22 '05 #3

P: n/a
"Test" <if******@mailnew.com> wrote...
Sorry, some people may have asked this question before. It is really hard to find relevant articles about this topic on the web using key words.

I know it is not recommended to use return statement in constructor.
Says who?
But,
1. How about a return statement without any return value? Does the execution of the constructor stop at the return statement?
It doesn't stop. It returns the control to the point from which the
constructor was invoked. If returning control to the caller you call
"stopping", then it stops.
2. How does the compiler handle the return statement with a value? Does it
just give a warning?


It should give an error message. The Standard specifically _prohibits_
any values in a return statement in a constructor body (12.1/12).

Victor
Jul 22 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.