A Question on Copy Constructor

This is a discussion on A Question on Copy Constructor within the C++ Programming forums, part of the General Programming Boards category; The copy constructor isn't called. Why? Here is the code. Code: #include<iostream> using namespace std; class Circle { private: int ...

  1. #1
    C/C++Newbie Antigloss's Avatar
    Join Date
    May 2005
    Posts
    212

    A Question on Copy Constructor

    The copy constructor isn't called. Why?

    Here is the code.
    Code:
    #include<iostream> 
    
    using namespace std; 
    
    class Circle 
    { 
    private: 
          int x,y; 
          int radius; 
    public: 
          Circle(); 
          Circle(const Circle &); 
    };
    
    Circle getCircle();
    
    int main() 
    { 
        Circle c; 
        c = getCircle(); 
        
        cin.get(); 
        return 0; 
    } 
    
    Circle::Circle() 
    { 
          x = y = radius = 30; 
    }
    
    Circle::Circle(const Circle &c) 
    { 
          cout << "copy constuctor!" << endl; 
          x=c.x;
          y=c.y;
          radius = c.radius; 
    }
    
    Circle getCircle() 
    { 
           Circle c; 
           return c; 
    }
    When getCircle() returns, it should call the copy constructor and generate a temporary object, right?
    But it comes out that the copy constructor is not call cause I cannot find `copy constuctor!' in the output.
    Last edited by Antigloss; 05-13-2006 at 10:39 PM.

  2. #2
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,246
    Your copy constructor should be called with that code.

  3. #3
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    Quote Originally Posted by Antigloss
    When getCircle() returns, it should call the copy constructor and generate a temporary object, right?
    Couldn't default ctor + assignment operator be the culprit?

    [C++ newb still reading this.]
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,602
    Couldn't default ctor + assignment operator be the culprit?
    I think that is it.

    Antigloss, why not just add print statements to the default constructor and copy assignment operator, just like what you have done for the copy constructor?
    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

  5. #5
    Registered User
    Join Date
    Jun 2005
    Posts
    6,254
    The C++ standard allows the compiler to eliminate temporary objects if the only way of detecting the existance of those temporaries is by tracking calls to constructors or destructors.

    Logically, the getCircle() function would create a temporary copy of c which would be returned to the caller. However, as the only way of detecting that temporary would be by tracking constructor and destructor calls ...... In this particular case, the compiler has implemented what is known as the "Return Value Optimisation".

  6. #6
    C/C++Newbie Antigloss's Avatar
    Join Date
    May 2005
    Posts
    212
    Thanks grumpy and also the other guys.
    Thank you very much!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Copy constructor question
    By BigFish21 in forum C++ Programming
    Replies: 9
    Last Post: 05-04-2008, 12:18 PM
  2. Template generated copy constructor
    By AverageSoftware in forum C++ Programming
    Replies: 8
    Last Post: 07-13-2007, 10:51 AM
  3. illegal copy constructor required?
    By ichijoji in forum C++ Programming
    Replies: 1
    Last Post: 03-08-2005, 05:27 PM
  4. Need help in classes
    By LBY in forum C++ Programming
    Replies: 11
    Last Post: 11-26-2004, 03:50 AM
  5. Copy Constructor crashing program
    By bob2509 in forum C++ Programming
    Replies: 5
    Last Post: 11-12-2002, 03:21 PM

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