Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Sign In with Facebook Sign In with Google Sign In with OpenID

Categories

We have migrated to a new platform! Please note that you will need to reset your password to log in (your credentials are still in-tact though). Please contact lee@programmersheaven.com if you have questions.
Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

Why do I need a copy constructor?

Hi All,



My basic question is what is the benefit I get by having a copy constructor? I mean from the optimization and efficiency point of view. Because ultimately insde the copy constructor you would be using the '=' assignment operator only so why not have only a overloaded '=' assignment operator instead of having a copy constructor.



In essence, why do I need a copy constructor? Can't I do with only an overloaded '=' operator?


Comments

  • : Hi All,

    :

    : My basic question is what is the benefit I get by having a copy constructor? I mean from the optimization and efficiency point of view. Because ultimately insde the copy constructor you would be using the '=' assignment operator only so why not have only a overloaded '=' assignment operator instead of having a copy constructor.

    :

    : In essence, why do I need a copy constructor? Can't I do with only an overloaded '=' operator?

    :



    A copy constructor is only used when you declare a class object and initialize it with an existing object of the same class. Therefore, they come into play if you plan on implementing a class constructor of the following form:



    MyClass::MyClass( MyClass Copy );



    Such as if I do this:



    CString MyString1( "Hello World!" );

    CString MyString2( MyString1 );



    In the above case, a copy constructor is called. The compiler should automatically provide both default copy constructors and assignment operators. For many people, the defaults will be sufficient, however there is trouble when dealing with classes that contain dynamically allocated members. Consider this example of a class:



    class MyClass

    {

    private:

        char *pMessage;

    public:

        MyClass( char * );

        ~MyClass();

    };



    Suppose the constructor dynamically allocates space for the argument, copies the argument's data into this space and assigns the resulting pointer to the pMessage member variable. Then if I do this:



    MyClass Class1( "My name is uncle bob." );



    the pMessage pointer gets the address of a dynamically allocated space holding the contents "My name is uncle bob." Now suppose I do this:



    MyClass Class2( Class1 );



    And I want the effect of this to be that Class2's

    data is copied from Class1's data. If I have not explicitly defined a copy constructor, then the provided default does a simple copy of all the data elements which will result in Class2's pMessage pointer pointing to the exact same space as Class1's pMessage pointer. Now, if Class1 goes out of scope and the destructor is called, what happens to Class2? Since it was pointing to a data area also owned by Class1, and since Class1 has been destroyed, Class2 probably has garbage in it. In this case, you must explicitly define a copy constructor such as this one:



    MyClass::MyClass( const MyClass& Copy )

    {

    int Length = strlen(Copy->pMessage) + 1;

    this->pMessage = new char[Length];

    memcpy( this->pMessage, Copy->pMessage, Length );

    }



    With this defined, Class2 would have gotten a completely new pointer pointing to a new memory area that holds the same character data as Class1's pointer. Then, when Class1 is destroyed, Class2 isn't affected since the memory locations pointed to by the pointers are not shared.



    Bottomline, if your class does not have any dynamically allocated elements, then the default copy constructor provided by the compiler will do just fine. If not, you must explicitly define one yourself if you plan on declaring class instances like I did up above with the 'CString' classes. Also, if you don't plan on using your classes where you would need to call a copy constructor, then of course you wouldn't need one. Kind of a long explanation, but there wasn't any other way I could think of at the moment to describe this.


Sign In or Register to comment.