Hello i want to create a new class variable that is a pointer and initialize it by copy constructor. Though I know how copy constructor works by refernce, i cannot figure out how to do it. Can you help me? For example I have this definition:
class A{
public:
int a;
private:
};
and in another code segment i do the following:
A *object= new A;
A->a=10;
A *newobject= new A(*object);
but i get a segmentation fault. Can you help me? I also tried:
A *newobject= new A(&(*object));
but it doesn't work either.
With this kind of simple example, the following which uses the default bit wise copy constructor works fine.
The simple class looks like
class Bclass {
public:
int iValue;
};
And the code to use the copy constructor looks like:
Bclass *pObject = new Bclass;
pObject->iValue = 10;
Bclass *pObject2 = new Bclass (*pObject);
Using Microsoft Visual Studio 2005, the above works fine.
See also Implementing a Copy Constructor.
See also Copy constructor for pointers to objects.