return statement in C++ constructor

T

Test

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.
 
D

David Harmon

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.
 
J

jeffc

Test said:
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?
 
V

Victor Bazarov

Test said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,156
Messages
2,570,878
Members
47,405
Latest member
DavidCex

Latest Threads

Top