Copy constructor and new

This is a discussion on Copy constructor and new within the C++ Programming forums, part of the General Programming Boards category; Consider the following code snippet: Code: List_Node node; List_Node *ptr; ptr = new List_Node(node); } Will the copy constructor for ...

  1. #1
    Registered User
    Join Date
    Mar 2009
    Posts
    399

    Copy constructor and new

    Consider the following code snippet:

    Code:
    List_Node node;
    List_Node *ptr;
    ptr = new List_Node(node);
    }
    Will the copy constructor for List_Node be activated when new is called? I have written a program that uses code similar to this, and although it seems to work, I have never seen copy constructors used like this in any books or online articles.

  2. #2
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,771
    No, the copy constructor is not called. The constructor is, though.
    Had ptr not been a pointer, then yes, the copy constructor would have been called.
    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.

  3. #3
    Registered User
    Join Date
    Mar 2009
    Posts
    399
    That's what I thought, but the thing is that I don't have a List_Node constructor that takes a List_Node object as an argument, so why does the code above work at all?

    The only constructor that matches the above call is the copy constructor, so surely it must be called then?

    Code:
    List_Node(const List_Node& ref); // Copy constructor
    Last edited by Memloop; 09-13-2009 at 09:08 AM.

  4. #4
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,771
    A class that takes a (const) reference to itself as argument is called a copy constructor.
    But you're dealing with pointers, which is a native type, hence no copy constructors are called.
    You are creating a new List_Node object, though, which is why the List_Node() constructor is called (not copy constructor).
    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
    Registered User
    Join Date
    Mar 2009
    Posts
    399
    But shouldn't the compiler complain that I'm calling the constructor with an argument that it's not declared for? And it doesn't explain why the constructor does everything my copy constructor is supposed to be doing when there's no such code in the constructor.

    I'm guessing g++ is doing something strange so that the program works, but at least I now know the code is not portable.

  6. #6
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    And just to elaborate on what Elysia said, references, pointers, and POD's don't really have constructors (there are some cases where C++ pretends that they do, but that's beside the point here).

  7. #7
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    Quote Originally Posted by Memloop View Post
    But shouldn't the compiler complain that I'm calling the constructor with an argument that it's not declared for? And it doesn't explain why the constructor does everything my copy constructor is supposed to be doing when there's no such code in the constructor.

    I'm guessing g++ is doing something strange so that the program works, but at least I now know the code is not portable.
    Ok, I misunderstood your question. If you defined a copy constructor, then yes, that is being called when the object is created. If not, then the default (eg: compiler generated) copy constructor is being called.

  8. #8
    The larch
    Join Date
    May 2006
    Posts
    3,573
    What are you talking about. Naturally the copy constructor is called:

    Code:
    List_Node node; //default constructor
    List_Node *ptr;
    ptr = new List_Node(node); //copy constructor
    And it works because the compiler automatically generates a default copy constructor for classes if you don't do so (default being shallow member-wise copy).
    Last edited by anon; 09-13-2009 at 09:31 AM.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  9. #9
    Registered User
    Join Date
    Mar 2009
    Posts
    399
    Thanks for clearing that up, but I find it strange that I have never seen a copy constructor used in combination with new in any C++ litterature or in online tutorials.

  10. #10
    The larch
    Join Date
    May 2006
    Posts
    3,573
    You can use any constructor in combination with new:

    Code:
    X* p = new X("Hello world", 3.14, 42);
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  11. #11
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,771
    Eh, I missed that it isn't actually just creating a new object, but making a copy of an existing object.
    Of course the copy constructor will be called then...
    Sorry about the confusion.
    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.

Popular pages Recent additions subscribe to a feed

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