Interview Question
Software Engineer / DevelopersI will tell you, many people working in the industry believe that "constructor" does return reference to the object", but what I feel that neither it returns nor it requires to return. As per C++ semantics it help creation and initialization of objects through "invocation of constructor by *this object implicitly, thus data members that get initialized belongs to *this object.
As per C++ standards 12.1.12
"No return type (not even void) shall be specified for a constructor. A return statement in the body of a constructor shall not specify a return value. The address of a constructor shall not be taken."
If we put a return statement in a Ctor, it will return from that point. The code below prints only "first"
struct A
{
A()
{
cout<<"first\n";
return;
cout<<"second\n";
}
};
int main()
{
A a;
return 0;
}
Well I think constructors are different than functions. We say that constructors even do not return void type therefore if the question is simply returning anything from constructor then the answer is simply a No
- DashDash October 17, 2010