Thread: A Question on Copy Constructor

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

    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,268
    Your copy constructor should be called with that code.

  3. #3
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    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
    28,413
    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?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    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
    216
    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, 06:27 PM
  4. Need help in classes
    By LBY in forum C++ Programming
    Replies: 11
    Last Post: 11-26-2004, 04:50 AM
  5. Copy Constructor crashing program
    By bob2509 in forum C++ Programming
    Replies: 5
    Last Post: 11-12-2002, 04:21 PM