When would you need to use a copy constructor

This is a discussion on When would you need to use a copy constructor within the C++ Programming forums, part of the General Programming Boards category; Hello, I am doing some reading on copy constructors. I know that the compiler will create a default copy constructor. ...

  1. #1
    UK2
    Join Date
    Sep 2003
    Posts
    112

    When would you need to use a copy constructor

    Hello,

    I am doing some reading on copy constructors. I know that the compiler will create a default copy constructor.

    But what is the real reason that people use copy constructors? This is the code I have been working with. I understand the coding ok.

    Code:
    class Mammal
    {
     public:
     Mammal():itsAge(1) { cout << “Mammal constructor...\n”; }
    virtual ~Mammal() { cout << “Mammal destructor...\n”; }
     Mammal (const Mammal & rhs);
     virtual void Speak() const { cout << “Mammal speak!\n”; }
     virtual Mammal* Clone() { return new Mammal(*this); }
     int GetAge()const { return itsAge; }
     protected:
     int itsAge;
    };
    Many thanks,

  2. #2
    Registered User
    Join Date
    Apr 2007
    Location
    Sydney, Australia
    Posts
    217
    For classes that have dynamic memory.

    Code:
    class string
    {
    private:
      char* data;
      size_t len;
    };
    If you let the compiler create a default copy constructor then it will copy the pointer for data over. You don't want this. The default copy constructor might look like this:

    Code:
    string::string(const string& str) {
      data = str.data;
      len = str.len;
    }
    They would be sharing the same block of memory.
    What you want is something like this:

    Code:
    class string
    {
    private:
      char* data;
      size_t len;
    
    public:
      string(const string& str) {
        len = str.len;
        data = new char[len];
        memcpy(data, str.data, len);
      }
    };

  3. #3
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,545
    When you want to override the default behavior for the copy constructor or when the compiler cannot create a default copy constructor for you (for example, you have constant member data in your class).
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  4. #4
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    or example, you have constant member data in your class
    Constant members prevent generation of a copy assignment operator, not of the copy constructor. Same for references.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  5. #5
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,545
    Right, didn't think of that, but the principle applies - sometimes the compiler cannot make a copy constructor or other constructors / operators.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  6. #6
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    I'd say the most prominent example is a member of std::auto_ptr, which is non-copyable.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  7. #7
    Registered User
    Join Date
    Apr 2006
    Posts
    137
    Remember that there is a default copy constructor so it should be fine for most classes unless yours has a lot of different variables and structure.
    ★ Inferno provides Programming Tutorials in a variety of languages. Join our Programming Forums. ★

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Copy constructor
    By dude543 in forum C++ Programming
    Replies: 26
    Last Post: 01-26-2006, 04:35 PM
  2. illegal copy constructor required?
    By ichijoji in forum C++ Programming
    Replies: 1
    Last Post: 03-08-2005, 05:27 PM
  3. Linked list copy constructor issue
    By Craptastic! in forum C++ Programming
    Replies: 1
    Last Post: 08-03-2003, 08:30 PM
  4. copy constructor
    By Eber Kain in forum C++ Programming
    Replies: 1
    Last Post: 09-30-2002, 05:03 PM
  5. Using strings with the copy constructor
    By Unregistered in forum C++ Programming
    Replies: 3
    Last Post: 08-29-2001, 03:04 PM

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