Copy Constructor in Class

This is a discussion on Copy Constructor in Class within the C++ Programming forums, part of the General Programming Boards category; Hi everybody, I'm having a really hard time learning about copy constructor. I have tried to read several books about ...

  1. #1
    Registered User
    Join Date
    Nov 2008
    Posts
    22

    Copy Constructor in Class

    Hi everybody,

    I'm having a really hard time learning about copy constructor. I have tried to read several books about copy constructor but seems like i'm stuck or something.
    For example: (the code is pulled out from the book. The book fails to explain it more specifically)

    Code:
    #include <iostream>
    #include <string>
    
    using namespace std;
    
    class my_string {
    public:
           my_string(): len(0)
              { s = new char[1]; assert(s != 0); s[0] = 0; }
           my_string(const my_string& str);
           my_string(const char* p);
           ~my_string() {delete []s; }
           void assign(const my_string& str);
           void print() const { cout << s << endl; }
           void concat(const my_string& a, const my_string& b);
    
    private:
            char* s;
            int len;
    };
    
    my_string::my_string(const char* p)
    {                                  
           len = strlen(p);
           s = new char[len + 1];
           assert(s != 0);
           strcpy(s, p);
    }
    
    my_string::my_string(const my_string& str) : len(str.len)
    {
           s = new char[len + 1];
           assert(s != 0);
           strcpy(s, str.s);
    }
    
    void my_string::assign(const my_string& str)
    {
           if (this == &str)
              return;
              delete []s;
              len = str.len;
              s = new char[len + 1];
              assert(s != 0);
              strcpy(s, str.s);
    }
    
    void my_string::concat(const my_string& a, const my_string& b)
    {
           char* temp = new char[a.len + b.len + 1];
           
           len = a.len + b.len;
           strcpy(temp, a.s);
           strcat(temp, b.s);
           delete []s;
           s = new char[len + 1];
           assert(s != 0);
           strcpy(s, temp);
    }
    Ok! my question is let's say for this part of code:
    Code:
    my_string::my_string(const my_string& str) : len(str.len)
    {
           s = new char[len + 1];
           assert(s != 0);
           strcpy(s, str.s);
    }
    what does the "str" variable mean? is it an object that behaves exactly as my_string?
    again, for "len(str.len)", I don't get this part. What does it do? (set length equal to the length of the object str?)

    Another question i wanna ask is:
    Code:
    my_string(const my_string& str);
    Code:
    void assign(const my_string& str);
    are both of these prototypes a copy constructor? the second one is not a constructor, but it has the same argument "(const my_string& str)". It really confuses me.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,303
    Quote Originally Posted by davewang
    what does the "str" variable mean? is it an object that behaves exactly as my_string?
    It is a const reference to a my_string object.

    Quote Originally Posted by davewang
    again, for "len(str.len)", I don't get this part. What does it do? (set length equal to the length of the object str?)
    It is part of the initialisation list, and you have guessed what it does correctly: initialise the len member variable to the contents of the len member variable of str.

    Quote Originally Posted by davewang
    are both of these prototypes a copy constructor?
    No.

    Quote Originally Posted by davewang
    the second one is not a constructor, but it has the same argument "(const my_string& str)".
    Yes, but as you noted it is not a 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

  3. #3
    Registered User
    Join Date
    Nov 2008
    Posts
    22
    i see, thanks

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    23,022
    The idea is that if you do:
    Code:
    myobject o1;
    myobject o2 = o1;
    ...then the myobject::myobject(const myobject& rhs) will be called and rhs will in this case be a reference to o2.
    So basically, the argument for a constructor is a reference to the object that is to be copied into the current instance.
    So you could see it as:
    o1.assign(o2);
    Last edited by Elysia; 02-26-2010 at 12:15 PM. Reason: Oops
    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.

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,303
    Eh, Elysia's example actually demonstrates the copy assignment operator, not 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

  6. #6
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    23,022
    I tend to mix those up... fixed.
    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.

  7. #7
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,308
    Don't bother with these lines:
    Code:
    assert(s != 0);
    The new call in C++ is never allowed to return NULL by the standard. If you run out of memory then an exception will be thrown instead, completely bypassing those asserts.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. template and friend class
    By black_spot1984 in forum C++ Programming
    Replies: 3
    Last Post: 10-21-2008, 06:50 PM
  2. Message class ** Need help befor 12am tonight**
    By TransformedBG in forum C++ Programming
    Replies: 1
    Last Post: 11-29-2006, 11:03 PM
  3. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 07:39 AM
  4. Warnings, warnings, warnings?
    By spentdome in forum C Programming
    Replies: 25
    Last Post: 05-27-2002, 07:49 PM
  5. class member access denied
    By chiqui in forum C++ Programming
    Replies: 2
    Last Post: 05-27-2002, 03:02 PM

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