Interview Question Software Engineer / Developers

  • -
    0
    of 0 votes
    7
    Answers

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

    - Dilip on October 17, 2010 Report Duplicate | Flag
    Software Engineer / Developer C++



Comment hidden because of low score. Click to expand.
2
of 2 vote

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 | Flag Reply
Comment hidden because of low score. Click to expand.
1
of 1 vote

I guess the constructor itself does not return anything, we use constructor in conjunction the new keyword and therefore the object instance is returned , if constructor was meant to return something then we could have used it as a function call,its the "new" which creates and returns.

- Gaurav M on October 18, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

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.

- Anonymous on October 19, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

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;
}

- Standards says it returns nothing on October 22, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

No need to research more.... Constructors do not return anything.... That's cent percent correct.... :)

- Geet on April 12, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

if u written obj in constuctor it will always call to that constructor again and again ..but u return *this ok

- prasad on March 14, 2012 | Flag
Comment hidden because of low score. Click to expand.
-1
of 1 vote

If they don't, then "new ABC()" will not return anything. But since it does, yes, constructors return a reference to the newly created object when called.

- Metta on October 18, 2010 | Flag Reply


Add a Comment
Name:

Writing Code? Surround your code with {{{ and }}} to preserve whitespace.

Books

is a comprehensive book walking you through every aspect of getting a job at a top tech company, while focuses on software engineering interviews.

Learn More

Videos

CareerCup's interview videos give you a real-life look at technical interviews. In these unscripted videos, watch how other candidates handle tough questions and how the interviewer thinks about their performance.

Learn More

Resume Review

Most engineers make critical mistakes on their resumes -- we can fix your resume with our custom resume review service. And, we use fellow engineers as our resume reviewers, so you can be sure that we "get" what you're saying.

Learn More

Mock Interviews

Our Mock Interviews will be conducted "in character" just like a real interview, and can focus on whatever topics you want. All our interviewers have worked for Microsoft, Google or Amazon, you know you'll get a true-to-life experience.

Learn More