# copy constructors and pointers

This topic is 4823 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

I was tired and not thinking straight when I was writing this class. I thought about having a copy constructor that takes a pointer of the same type. General header copy belowed. The compiler gave me errors. Why is this a bad or wrong? I'm at work so I don't have the exact code or error msg.
class box
{
public:
box();
box(int x, int y);
box(box cp_box);
box(box * cp_ptr_box);//<------is this wrong?
~box(;
private:
//members
};



##### Share on other sites
A copy constructor can have only one of four function signatures: box(box & cp_box);, box(const box & cp_box);, box(volatile box & cp_box); or box(const volatile box & cp_box);. Any other constructor is by definition not a copy constructor.

In particular, the box(box cp_box); function signature is bad news. You can't pass a box object by value to the constructor; you need to use a reference of some sort.

##### Share on other sites
It's not particularely wrong nor bad, it's just not a copy constructor.

A copy constructor looks like this:

class CMyClass{  public:    CMyClass( const CMyClass& rhsClass );};

Also, only supply a copy constructor if you have a good reason (something like it needs to create new pointer instances), and then also supply assignment operator and destructor (rule of the three).
If you don't actually need a copy constructor the compiler will provide one automatically.

edit: SiCrane's been faster. I never used the other forms myself.

##### Share on other sites
Quote:
 Original post by devgooruclass box{ public: box(); box(int x, int y); box(box cp_box); box(box * cp_ptr_box);//<------is this wrong? ~box(; // <-- if it *is* like this, you're missing ')' :) private: //members};

A proper copy constructor takes an actual object, not a pointer (although it needs to be passed by reference: "box &" - I'll trust SiCrane on this one, although I can't think why passing by value oughtn't work...). However, as far as I know, there's no reason you can't define an ordinary constructor with that signature - it just won't do what you want. Perhaps the error comes from when you try to use it?

##### Share on other sites
Ok, that sound good. This s*cks when you pay $20g a year and they don't tell you this kinda stuff. My teacher never specified that we should write constructor in that nature and never said "box(box cp_box)" would be bad. Thanks$20g worth..

##### Share on other sites
Quote:
 Original post by ZahlmanA proper copy constructor takes an actual object, not a pointer (although it needs to be passed by reference: "box &" - I'll trust SiCrane on this one, although I can't think why passing by value oughtn't work...).

Very simple—because when you pass by value, you make a copy of the object, and this is done using the copy constructor. Therefore, if you attempt to pass by value to the copy constructor, it must invoke the copy constructor, which must invoke the copy constructor ...

##### Share on other sites
Quote:
 Original post by Zahlman However, as far as I know, there's no reason you can't define an ordinary constructor with that signature - it just won't do what you want.

You can pass by value for constructors that don't take a single argument. That is to say something like box(box cp_box, int x) is valid. It's just passing a box by value when it's the only argument that's non-kosher.

##### Share on other sites
Quote:
Original post by SiCrane
Quote:
 Original post by Zahlman However, as far as I know, there's no reason you can't define an ordinary constructor with that signature - it just won't do what you want.

You can pass by value for constructors that don't take a single argument. That is to say something like box(box cp_box, int x) is valid. It's just passing a box by value when it's the only argument that's non-kosher.

Aha, because that one wouldn't be able to recurse as Miserable described, got it. [smile]