Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Calling constructor in return statement

Tags:

c++

visual-c++

class test{

public:

    int data;
    test(const test& ){cout<<"INSIDE COPY CON "<<endl;}
    test(int val = 0) : data(val){ cout<<"INSIDE CON "<<endl; }

    test testfun(const test& obj)
    {
        cout<<"data : "<<data<<endl;
        //test test3(this->data + obj.data);
        //cout<<"test3 :"<<test3.data<<endl;
        //return test3;   //This will work only if return type is changed to const ref
        return test(data + obj.data); 


    }
};

int main()
{

    test testO1(1);
    test testO2(2);
    test testO3 = testO1.testfun(testO2);

    cout<<testO3.data<<endl;


    getchar();


}

OUTPUT:

INSIDE CON

INSIDE CON

data : 1

INSIDE CON

3

What happens when constructor is called in return statement? Since i am able to return by value and it works i think its not a temporary location. OR is it creating the object as a temporary and using copy constructor to copy the values , inthat case why is the print inside the copy constructor not getting printed.

like image 441
hackrock Avatar asked Jun 08 '12 00:06

hackrock


People also ask

Can we use return statement in constructor?

You cannot declare a constructor as virtual or static , nor can you declare a constructor as const , volatile , or const volatile . You do not specify a return type for a constructor. A return statement in the body of a constructor cannot have a return value.

Does a constructor need a return statement?

Usually, constructors do not have a return statement. Their task is to write all necessary stuff into this , and it automatically becomes the result.

Does return by value call copy constructor?

The copy constructor is called because you call by value not by reference. Therefore a new object must be instantiated from your current object since all members of the object should have the same value in the returned instance.

How many return statement can be used in a constructor?

All Answers (1) By definition there is no possibility of returning a value from a constructor. A constructor does not support any return type.


2 Answers

It creates a temporary object, which then gets copied to the return value.

But for efficiency, C++ allows calls to the copy constructor (or the move constructor in C++11) to be elided, so you should not rely on side-effects of copy constructors occurring.

See Return Value Optimization and Want Speed? Pass By Value

like image 125
Jonathan Wakely Avatar answered Oct 10 '22 03:10

Jonathan Wakely


You ask a penetrating question.

It is up to the compiler and the associated toolchain to decide what to do, but the basic pattern is as follows. Before calling testfun(), the caller [main(), in your example] reserves space for test03 on the stack. It then passes the address of test03 to testfun().

The function testfun() must somehow put a test object at the address the caller has supplied. If the function has only one return statement, it is free to use the storage provided by the caller to build the return value. It need not use its own storage. It can use main()'s.

Now, this strategy does not always work. It typically fails when a function like testfun() has two, distinct return statements, one or both of which return not a temporary but a named object. In that case, the compiler is forced to do an otherwise unnecessary copy-on-return. However, the more usual case resembles yours, in which testfun() just builds the return value directly in the spot in which main() wants it. In this case, no actual copy occurs.

It is thus up to the compiler to decide whether the copy constructor is called on return in a case like this.

like image 33
thb Avatar answered Oct 10 '22 02:10

thb