Interview Question Software Engineer / Developers
0of 0 votesThis is my biggest doubt, i feel people keep different ideas. The question is:
Does constructor return anything? Yes/No. Justify.
Any person who has really done some research on that, let me know it. Some technical people think..YES(returns object of same type). some say NO......I feel NO and I have done some research. Please help me.
I 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;
}
No need to research more.... Constructors do not return anything.... That's cent percent correct.... :)

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 on October 17, 2010 Edit | Flag Reply