Click here to Skip to main content
15,907,497 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi every one.
First I want to apologize if that question was asked before but I couldn't find the answer for it :(.



I need to know how can I have two classes as variables in each other. I know I have to have forward reference and the variables to be pointes. But can someone show me how should a deep copy constructor, operator= and dereference function should look like in that instance. Pointers for me a little bit hard sometimes.

Todor
Posted

 
Share this answer
 
Comments
xumepoc 3-Apr-15 9:12am    
My bigger problem in the deep copy constructor and operator= should I do:

delete pointer;
pointer = new Pointer();
pointer = cop.pointer;

where "pointer" is a pointer variable, and the cop is the (const Pointer& cop).

Also I should delete the pointer on the deconstructor, right?
Richard MacCutchan 3-Apr-15 9:16am    
I am not sure exactly what you mean there. Take a look at https://www.google.com/search?q=c%2B%2Bcopy+constructor for some useful samples.
xumepoc 3-Apr-15 9:50am    
Sorry I am really bad in explaining thnings :D

That is what I mean:

class Boo;

class Foo
{
public:
Foo();
virtual ~Foo();
Foo(const Foo& cop);
Foo& operator=(const Foo& cop);

private:
Boo* b;
}

#include "Boo.h"
Foo::Foo()
{
}
Foo::~Foo()
{
delete b;
}

Foo:Foo(const Foo& cop)
{
delete b;
b = new Foo();
b = cop.b;
}

Foo& Foo::operator=(const Foo& cop)
{
delete b;
b = new Foo();
b = cop.b;

return *this;
}

class Foo;
class Boo
{
public:
Boo();
virtual ~Boo();
Boo(const Boo& cop);
Boo& operator=(const Boo& cop);

private:
Foo* f;
}

#include "Foo.h"
Boo::Boo()
{
}
Boo::~Boo()
{
delete f;
}

Boo:Boo(const Boo& cop)
{
delete b;
f = new Boo();
f = cop.f;
}

Boo& Boo::operator=(const Boo& cop)
{
delete f;
f = new Foo();
f = cop.f;

return *this;
}

Is this correct or there are problems. I know that I have to pass either f or b as a reference because I will create a loop.
Richard MacCutchan 3-Apr-15 11:12am    
I am not realy sure what problem you are trying to solve, but it would be a good idea to build the code and do some sample testing.
xumepoc 3-Apr-15 12:10pm    
My problem is that I need the child variable (in the example above Boo *b) to have access to the parent Foo.
I will post a sample code later tonight.
Judging by your code you don't understand the basics of pointers. Please consult this article: A Beginner's Guide to Pointers[^]

Also, you are copying objects needlessly. The point of pointers (pun not intended) is that you don't need to copy; instead you refer to the same object from different locations. A copy is only needed if you want to change the object locally and don't want that change to affect the original object.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900