constructor call return

This is a discussion on constructor call return within the C++ Programming forums, part of the General Programming Boards category; Code: class test { public: test() }; void main() { test t = test(); // what is returned here? } ...

  1. #1
    Registered User
    Join Date
    Oct 2011
    Posts
    48

    constructor call return

    Code:
    class test
    {
    public:
    test()
    
    };
    
    void main()
    {
    
    test t = test();   // what is returned here?
    
    }

    Since constructor do not have return type. Then what is returned in t?

  2. #2
    Registered User antred's Avatar
    Join Date
    Apr 2012
    Location
    Germany
    Posts
    255
    When you write

    Code:
    test t = test();
    two things are happening:

    1) test() creates a temporary of type test.
    2) The test t = part creates a second instance of type test. This instance t is created via the class's copy constructor (test t = test(); is basically the same as test t( test() );). The argument for t's copy constructor is the temporary created in step 1.


    EDIT: Note that in your case, just test t; would be sufficient. This would create instance t via test's default constructor.

    EDIT #2: In C++ and C, it should always be int main(), not void main().
    Last edited by antred; 05-27-2012 at 04:43 PM.

  3. #3
    Registered User
    Join Date
    Oct 2011
    Posts
    48
    is basically the same as test t( test() );
    I didn't understand this point.

    In my opnion it should be

    test t(t); // This syntax is incorrect. because you cannot pass same object in constructor; But I thought default copy constructor would require this.

    Please clarify.

  4. #4
    Registered User antred's Avatar
    Join Date
    Apr 2012
    Location
    Germany
    Posts
    255
    A copy constructor requires as its argument another instance of the same type. The expression test() generates a temporary that satisfies that requirement.

  5. #5
    Registered User antred's Avatar
    Join Date
    Apr 2012
    Location
    Germany
    Posts
    255
    Trying playing around with this program a bit.

    Code:
    #include <iostream>
    
    class test
    {
    public:
        test()
        {
            std::cout << "Creating test instance at 0x" << std::hex << this << std::dec << " via default c'tor.\n";
        }
        
        ~test()
        {
            std::cout << "Destroying test instance at 0x" << std::hex << this << std::dec << ".\n";
        }
    
        test( const test& t )
        {
            std::cout << "Creating test instance at 0x" << std::hex << this << std::dec << " via copy c'tor. Address of source instance is 0x"
                << std::hex << &t << std::dec << ".\n";
        }
    
        test& operator = ( const test& t )
        {
            std::cout << "Copy assigning to test instance at 0x" << std::hex << this << std::dec << ". Address of source instance is 0x"
                << std::hex << &t << std::dec << ".\n";
    
            return *this;
        }
    };
    
    
    int main()
    {
        test t = test();
        t = test();
    }
    Compile without optimizations.


    P.S. The program might actually not use the copy constructor at all. This is because your compiler is allowed to remove the copy if it finds that it is unnecessary. This is called copy elision: http://en.wikipedia.org/wiki/Copy_elision
    Last edited by antred; 05-27-2012 at 05:20 PM.

  6. #6
    Registered User
    Join Date
    Oct 2011
    Posts
    48
    Output:
    Code:
    Creating test instance at 0x002AF7FB via default c'tor.
    Destroying test instance at 0x002AF7FB.
    Press any key to continue . . .
    I don't know why copy constructor is not called?

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,313
    Quote Originally Posted by freiza
    I don't know why copy constructor is not called?
    You may have missed antred's postscript:
    Quote Originally Posted by antred
    P.S. The program might actually not use the copy constructor at all. This is because your compiler is allowed to remove the copy if it finds that it is unnecessary. This is called copy elision: Copy elision - Wikipedia, the free encyclopedia
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  8. #8
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,308
    Quote Originally Posted by freiza View Post
    test t(t); // This syntax is incorrect. because you cannot pass same object in constructor; But I thought default copy constructor would require this.
    C++ doesn't support reincarnation constructors.

    Oh, and in case you missed it, that was a joke.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Bad constructor call?
    By Elysia in forum C++ Programming
    Replies: 4
    Last Post: 12-18-2010, 05:17 PM
  2. Specialized Constructor call Default Constructor
    By threahdead in forum C++ Programming
    Replies: 15
    Last Post: 08-23-2010, 04:39 PM
  3. Call constructor
    By George2 in forum C++ Programming
    Replies: 4
    Last Post: 03-24-2008, 03:16 AM
  4. C++ have a constructor call another constructor
    By QuestionC in forum C++ Programming
    Replies: 4
    Last Post: 05-17-2007, 02:59 AM
  5. Call constructor within constructor. How to?
    By xErath in forum C++ Programming
    Replies: 10
    Last Post: 11-18-2004, 05:30 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21