c++ Using const in a copy constructor?

Posted by Anton on Stack Overflow See other posts from Stack Overflow or by Anton
Published on 2012-03-23T23:22:10Z Indexed on 2012/03/23 23:29 UTC
Read the original article Hit count: 234

Filed under:
|

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);

© Stack Overflow or respective owner

Related posts about c++

Related posts about copy-constructor