Like Tree2Likes
  • 1 Post By grumpy
  • 1 Post By whiteflags

copy constructor c++

This is a discussion on copy constructor c++ within the C++ Programming forums, part of the General Programming Boards category; will copy constructor does object initilization using another already created object? I understand that it can be applied for object ...

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

    Thumbs up copy constructor c++

    will copy constructor does object initilization using another already created object? I understand that it can be applied for object initilization and not for assignment. Is it correct? please clarify me

  2. #2
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    1,623
    If I understand correctly, the following is correct:
    Code:
    Object obj1(whatever);
    Object obj2(obj1);
    assignment also works fine:
    Code:
    obj2 = obj1
    Devoted my life to programming...

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,598
    Quote Originally Posted by leo2008
    will copy constructor does object initilization using another already created object?
    Normally yes, but of course what the copy constructor actually does depends on its implementation.

    Quote Originally Posted by leo2008
    I understand that it can be applied for object initilization and not for assignment.
    By definition, the copy constructor is invoked for copy construction, not assignment.
    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

  4. #4
    Registered User
    Join Date
    Jun 2005
    Posts
    6,252
    The wrinkle is that the statement
    Code:
       // assume b is of type Object
       Object a = b;
    also uses the copy constructor to initialise a, and does not invoke operator=().

    However
    Code:
       // assume b is of type Object
       Object a;
       a = b;
    uses the default constructor to construct a, followed by an assignment (operator=()).

    Further wrinkles are that there are ways in which some or all of these behaviours may be prevented or (for want of a better word) adjusted. The examples above assume

    1) All of the constructors and operators mentioned are defined (whether implicitly or explicitly) and accessible to the code above.
    2) The constructors have not been declared using the explicit keyword.
    King Mir likes this.
    Right 98% of the time, and don't care about the other 3%.

  5. #5
    Registered User
    Join Date
    Nov 2008
    Posts
    162
    Quote Originally Posted by grumpy View Post
    The wrinkle is that the statement
    Code:
       // assume b is of type Object
       Object a = b;
    also uses the copy constructor to initialise a, and does not invoke operator=().

    However
    Code:
       // assume b is of type Object
       Object a;
       a = b;
    uses the default constructor to construct a, followed by an assignment (operator=()).

    Further wrinkles are that there are ways in which some or all of these behaviours may be prevented or (for want of a better word) adjusted. The examples above assume

    1) All of the constructors and operators mentioned are defined (whether implicitly or explicitly) and accessible to the code above.
    2) The constructors have not been declared using the explicit keyword.
    do you mean to say here that the copy cosntructor overrides the default bit wise copy by parameters?

  6. #6
    Registered User
    Join Date
    Jun 2005
    Posts
    6,252
    Quote Originally Posted by leo2008 View Post
    do you mean to say here that the copy cosntructor overrides the default bit wise copy by parameters?
    I neither said nor suggested any such thing.
    Right 98% of the time, and don't care about the other 3%.

  7. #7
    Registered User
    Join Date
    Nov 2008
    Posts
    162
    Quote Originally Posted by grumpy View Post
    I neither said nor suggested any such thing.
    Whats your opinion on copy constructors overiding default bitwise copy?

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,598
    Huh? What are you talking about now? What is your actual question?
    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

  9. #9
    Registered User
    Join Date
    Nov 2008
    Posts
    162
    Quote Originally Posted by laserlight View Post
    Huh? What are you talking about now? What is your actual question?
    I want to know whether copy constructor will override the defalut bit wise copy by parameters, when run?

  10. #10
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,660
    "Bitwise copy constructors" doesn't mean anything to me sorry.

    There can only be one copy constructor, because the copy constructor has a specific function signature.

    T(const T& rhs);

    That's it.

    Ordinarily, a copy constructor that a compiler would generate is just one that calls the other copy constructors for each data member in the class. The default copy constructor usually works: Plain old data types have copy constructors, as do many STL types. Where you run into trouble is when your class contains raw pointers.

    In the default copy constructor, a raw pointer member simply copies the pointer, such that the new pointer member will have the same address value as the older pointer. This is called a "shallow copy". The worry is that one of the objects will fall out of scope and get destroyed, thus destroying the memory the pointer points to, and leaving the other pointer that shares that memory dangling.

    So, to solve that issue, you can rewrite the copy constructor to do something else. Perhaps use new when you construct the new object and so on, which is called a "deep copy".

    There are other reasons to rewrite the copy constructor, just so you know. You have to rewrite whenever the default behavior is not what you want. There are such semantics as copy-on-write and more. I don't really want to get into that area if it's not necessary. The bottom line is, if you write a copy constructor, that one will be used. If you don't write a copy constructor, then the default one will be generated, and that one will be used.

    Consider also the rule of three.
    Last edited by whiteflags; 03-17-2013 at 07:51 PM.
    stahta01 likes this.

  11. #11
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,299
    Quote Originally Posted by leo2008 View Post
    I want to know whether copy constructor will override the defalut bit wise copy by parameters, when run?
    Your question is based on an incorrect assumption.
    Whilst simple examples of where the copy-constructor is omitted may appear to perform bit-wise copy of the members, it in fact does not.

    It does member-wise copy. E.g. if one of the members is a std::string, then it will call the std::string copy-constructor to copy that string, which will likely involve memory allocation etc. Only those members that are built-in types will effectively result in a bit-wise copy.
    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. GCC and Copy Constructor
    By NA9x2000 in forum C++ Programming
    Replies: 7
    Last Post: 08-25-2012, 09:38 AM
  2. Copy constructor and new
    By Memloop in forum C++ Programming
    Replies: 10
    Last Post: 09-13-2009, 10:23 AM
  3. When would you need to use a copy constructor
    By steve1_rm in forum C++ Programming
    Replies: 6
    Last Post: 11-17-2008, 02:28 PM
  4. copy constructor
    By Eber Kain in forum C++ Programming
    Replies: 1
    Last Post: 09-30-2002, 05:03 PM
  5. copy constructor
    By Unregistered in forum C++ Programming
    Replies: 2
    Last Post: 04-30-2002, 02:23 PM

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