1

I have never written copy constructor, so in order to avoid pain i wanted to know if what i have coded is legit. It compiles but i am not sure that it works as a copy constructor should.

Also do i have to use const in the copy constructor or i can simply drop it. (What i dont like about const is that the compiler cries if i use some non const functions).

//EditNode.h
class EditNode
{
      explicit EditNode(QString elementName);
      EditNode(const EditNode &src);
}

//EditNodeContainer.h
class EditNodeContainer : public EditNode
{
      explicit EditNodeContainer(QString elementName);
      EditNodeContainer(const EditNodeContainer &src);
}

//EditNodeContainer.cpp
EditNodeContainer::EditNodeContainer(QString elementName):EditNode(elementName)
{
}       

//This seems to compile but not sure if it works
EditNodeContainer::EditNodeContainer(const EditNodeContainer &src):EditNode(src)
{

}


//the idea whould be to do something like this
EditNodeContainer *container1 = new EditNodeContainer("c1");
EditNodeContainer *copyContainer = new EditNodeContainer(container1);
3
  • Your copy constructor doesn't do anything that the compiler-generated one doesn't do. Commented Mar 23, 2012 at 23:24
  • Is there some reason why you need to write a custom copy constructor? In many cases, the default one should be fine. Commented Mar 23, 2012 at 23:25
  • i.e. when you want to copy data pointed from a member and not the pointer itself Commented Mar 23, 2012 at 23:27

3 Answers 3

3

A copy constructor is a constructor that has one of the following signatures:

class A
{
    A(A& other);
    //or
    A(const A& other);
    //or
    A(volatile A& other);
    //or
    A(const volatile A& other);
    //or any of the above + other parameters that have default arguments
    //example:
    A(const A& other, int x = 0) //this is also a copy constructor
};

The above is specified in 12.8.2 of the standard - C++03. so you are implementing correctly a copy constructor.

The reason it should receive a const parameter is that you're not changing the object you're copying from. If you call non-const functions on it, you're doing something wrong.

Also, in your snippet

EditNodeContainer *container1 = new EditNodeContainer("c1");
EditNodeContainer *copyContainer = new EditNodeContainer(container1);

you're not calling a copy constructor, because you're passing an EditNodeContainer* as parameter, not a EditNodeContainer.

Sign up to request clarification or add additional context in comments.

Comments

1

You're missing one * symbol. Copy constructor expects reference to object, but is given pointer to object. Just replace container1 with *container1 as parameter of copy constructor.

1 Comment

you are right, since container1 is a pointer so he need to de reference it
0

The parameter of a copy constructor can be an lvalue reference to non-const or an lvalue reference to const, but in practice it is always a reference to const (the deprecated auto_ptr is an exception).

You should not write a copy constructor unless you have to and you fully understand the consequences. If you are consequent in using RAII classes everywhere, you rarely need a custom copy constructor (unless you are writing a RAII class).

Also, please avoid raw pointers and new wherever possible.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.