CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 11 of 11
  1. #1
    Join Date
    Jun 2008
    Location
    United States
    Posts
    71

    Need help with simple copy constructor

    I'm a little confused about copy constructors and could use some help. Here's my situation: I have two classes, A and B. Class B is a child of class A. Class A looks something like this:

    Code:
    class A
    {
    public:
       A::A(); // constructor
       
       // some other functions
    
    private:
    char* name; // the name of the object is stored in a character array
    };
    Class B just has some more functions and more data. My question is how do I properly implement a copy constructor, since class A has it's name stored in the heap? I know a copy constructor is needed, but I'm not quite sure how to do it. If I create a copy constructor and inside it dynamically allocate memory and copy the name over for the new object, do I also have to copy all of the data members over?

    Thanks for your advice

  2. #2
    Join Date
    Aug 2008
    Posts
    902

    Re: Need help with simple copy constructor

    Well, a good way to handle this would be like so:

    Code:
    class A
    {
    public:
       A::A(); // constructor
       
       // some other functions
    
    private:
    std::string name; // the name of the object is stored in a character array
    };
    There. Now you don't even need a copy constructor, because the default copy constructor will suffice. You also don't have to worry about writing an assignment operator or a destructor, which you would have if your class used a char* with dynamically allocated memory.

    Isn't the standard template library great?

  3. #3
    Join Date
    Jun 2008
    Location
    United States
    Posts
    71

    Re: Need help with simple copy constructor

    Isn't the standard template library great?
    Yeah, but this is for a class and I'm supposed to demonstrate that I know how to use a copy constructor

  4. #4
    Join Date
    Apr 1999
    Posts
    27,449

    Re: Need help with simple copy constructor

    Quote Originally Posted by Robotics Guy View Post
    I'm a little confused about copy constructors and could use some help. Here's my situation: I have two classes, A and B. Class B is a child of class A. Class A looks something like this:

    Code:
    class A
    {
    public:
       A::A(); // constructor
       
       // some other functions
    
    private:
    char* name; // the name of the object is stored in a character array
    };
    Class B just has some more functions and more data. My question is how do I properly implement a copy constructor, since class A has it's name stored in the heap? I know a copy constructor is needed, but I'm not quite sure how to do it.
    Right, so isn't that what you're homework assignment is about, finding out how to do this? If we tell you, wouldn't we be giving you the answer?
    If I create a copy constructor and inside it dynamically allocate memory and copy the name over for the new object,
    Are you talking about a copy constructor for class A or class B? You should forget about B and create one for A first.

    And yes, when you create a copy constructor, it better be just that -- a copy. In other words, if you have two objects that are copies of each other, if you use one object in a program, the program must behave exactly the same as if you used the other object. This is easily achieved by copying all the members over to the copied class.

    Otherwise, your program will have very hard-to-find bugs if you are saying two objects are copies when they're not.

    Regards,

    Paul McKenzie

  5. #5
    Join Date
    Jun 2008
    Location
    United States
    Posts
    71

    Re: Need help with simple copy constructor

    Well, one thing I'm confused about is which class the copy constructor should exist in. The parent class is abstract, so does that mean I should make the copy constructor be in the child class? (I should note that I have several classes derived from the parent class).

  6. #6
    Join Date
    Apr 1999
    Posts
    27,449

    Re: Need help with simple copy constructor

    Quote Originally Posted by Robotics Guy View Post
    Well, one thing I'm confused about is which class the copy constructor should exist in. The parent class is abstract, so does that mean I should make the copy constructor be in the child class? (I should note that I have several classes derived from the parent class).
    So how does the derived classes have access to "name" in the base class? The "name" variable is private, and I see no member functions to access this value or maintain it in any way.

    Regards,

    Paul McKenzie

  7. #7
    Join Date
    Jun 2008
    Location
    United States
    Posts
    71

    Re: Need help with simple copy constructor

    There are get and set functions in the base class. I could also make it protected if that would help.

    I tried adding:

    Code:
    B::B(const B &copyMe);
    to the .h and:

    Code:
    B::B(const B &copyMe)
    {
    	setName(copyMe.getName()); // setName and getName are in the base class; they take and return a string, respectively. 
    }
    Where setName() is:

    Code:
    void A::setName(string newName)
    {
    	delete [] name; // delete the old name
    
    	name = new char[newName.size()+1];
    
    	for(int i = 0; i < newName.size(); i++) // copy characters from string over to char array
    		*(name+i) = newName[i];
    
    	*(name+newName.size()) = 0; // add the null terminated character as the last character
    }

  8. #8
    Join Date
    Jun 2008
    Location
    United States
    Posts
    71

    Re: Need help with simple copy constructor

    Never mind. I'm just going to try making a copy constructor for each child class. Hopefully that's what I'm supposed to do

  9. #9
    Join Date
    Apr 1999
    Posts
    27,449

    Re: Need help with simple copy constructor

    Quote Originally Posted by Robotics Guy View Post
    There are get and set functions in the base class. I could also make it protected if that would help.

    I tried adding:

    Code:
    B::B(const B &copyMe);
    to the .h and:

    Code:
    B::B(const B &copyMe)
    {
    	setName(copyMe.getName()); // setName and getName are in the base class; they take and return a string, respectively. 
    }
    This is not correct. What if copying A correctly was much more complex than calling setName()? The derived class shouldn't know or care how A makes a copy of itself, as long as the copy of A is correct. Regardless of whether A is abstract or not, you should implement the copy constructor in A. Nothing stops an abstract class from having a copy constructor.

    Once you do that, then all you need to do is call the copy constructor for A inside the B copy constructor:
    Code:
    B::B(const B &copyMe) : A(copyMe) 
    {
        // any other code that B needs to do for itself to copy B stuff
    }
    Now the code above is saying "I have an object that is an A, so A, make a copy of yourself with the object passed to me". In other words, B is not involved at all in making the copies for A, but B must call the A copy constructor so that the entire copy is done.

    Second, if you have a copy constructor, you should code an assignment operator. It makes no sense to be able to do this:
    Code:
    B b1;
    B b2 = b1;
    and not be able to do this:
    Code:
    B b1;
    B b2;
    b2 = b1;
    The former calls the copy constructor, the latter calls the assignment operator.

    Regards,

    Paul McKenzie
    Last edited by Paul McKenzie; December 5th, 2011 at 10:31 PM.

  10. #10
    Join Date
    Jun 2008
    Location
    United States
    Posts
    71

    Re: Need help with simple copy constructor

    Great advice, thank you.

    I have a question though. Let's say that I have base class A, then derived class B, then class C which is derived from B. If I write a copy constructor for C, do I call the copy constructor for B which in turn calls the copy constructor for A?

  11. #11
    Join Date
    May 2001
    Location
    Germany
    Posts
    1,158

    Re: Need help with simple copy constructor

    Yes.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured