define a copy constructor in a class having data member as an object of another class

D

dalu.gelu

Hi,
can anyone help me by writing a sample code of defining a copy
constructor in a class having data member as an object of another
class.
for eg:
class A{
int x;
public: A(){ x=6;}
};

class B{
A a1;
public:B(B &b1)
{ ???} //how i can assign the data members??
};

Any idea ...Thanks
 
V

VJ

Hi,
can anyone help me by writing a sample code of defining a copy
constructor in a class having data member as an object of another
class.
for eg:
class A{
int x;
public: A(){ x=6;}
};

class B{
A a1;
public:B(B &b1)
{ ???} //how i can assign the data members??
};

Any idea ...Thanks

1) add a public method to class A to assign a value to x
2) make class B friend of class A
 
G

Greg

can anyone help me by writing a sample code of defining a copy
constructor in a class having data member as an object of another
class.
for eg:
class A{
int x;
public: A(){ x=6;}
};

class B{
A a1;
public:B(B &b1)
{ ???} //how i can assign the data members??
};

Like so:

class B
{
public:
B( B& b1 )
: a1( b1.a1 )
{
}
};

Greg
 
G

Greg

Hi,
can anyone help me by writing a sample code of defining a copy
constructor in a class having data member as an object of another
class.
for eg:
class A{
int x;
public: A(){ x=6;}
};

class B{
A a1;
public:B(B &b1)
{ ???} //how i can assign the data members??
};

Like so:

class B
{
A a1;

public:
B( B& b1 ) : a1( b1.a1 )
{ }
};

Greg
 
G

Greg

thanks,

if the situation is so:
class B{
A *a1;
public:
B(B &b1){???}

Pretty much as one might expect:

class B
{
A *a1;
public:
B( B& b1)
{
a1 = b1.a1;
}
};

Note that "a1", even as a pointer member, can still be initialized the
same way as before (in the member list initializer of the contructor)
instead of within the body of B's construtor - as shown here.

When possible, it's better to initialize a class member in the
constructor's member-initializer list than in its body.

Greg
 
G

Greg

thanks,

if the situation is so:
class B{
A *a1;
public:
B(B &b1){???}

This case is trickier since a1 is now a pointer. The question is
whether to make a "deep copy" or a "shallow copy" of B. A deep copy
also creates a copy of the A object that a1 points to - and then
assigns the copied A object's address to the a1 member pointer in B's
copy. A shallow copy would just copy the a1 pointer itself - meaning
that both the original and copied B object would each have an a1 member
pointing to the same A object - which can be a problem if B is supposed
to delete the A object at some point (more on that problem later).

In terms of advantages, though, a shallow copy is more efficient and
economical than making a deep copy. Furthermore, a shallow copy of B
looks easy enough to implement:

class B
{
A *a1;
public:
B( B& b1)
{
a1 = b1.a1;
}
};

....but as mentioned above can be very difficult to maintain correctly,
now that a1 is shared. So to manage a shallow copy safely, B should
turn its a1 member into a "smart pointer" such as
std::tr1::shared_ptr<A>.

Note that "a1", even as a pointer member, can still be initialized the
same way as before (in the member list initializer of the constructor)
instead of within the body of B's constructor - as shown here. When
possible, it's better to initialize a class member in the constructor's
member-initializer list than in its body.

Greg
 
D

dalu.gelu

Thanks Greg, its a wonderful explanation..

its fine in compiling and linking the program in case of shallow copy u
provided
class B
{
A *a1;
public:
B( B& b1)
{
a1 = b1.a1;
}
};

but when i try to run it ends with some memory unhandled exception, its
perhaps due to shallow copy where it only copies the memory address not
the corresponding value.

ok then i hope the correct answer for tthe problem is to make it a deep
copy. could u please add some more lines in the above code so that the
output will be ok.
 
R

Ron Natalie

Thanks Greg, its a wonderful explanation..

its fine in compiling and linking the program in case of shallow copy u
provided
class B

but when i try to run it ends with some memory unhandled exception, its
perhaps due to shallow copy where it only copies the memory address not
the corresponding value.

ok then i hope the correct answer for tthe problem is to make it a deep
copy. could u please add some more lines in the above code so that the
output will be ok.
We can't answer your question without knowing what A is, and how a1
was set initially. Most likely your problem is less with the shallow
copy, but more with the fact that the object pointed by a1 is
mismanaged (deleted twice, deleted while people are still using it,
etc...).

Further the rule of three applies very much here. If you need to
define a copy constructor, a copy-assignment operator, or a destructor,
you probably need to define all three.
 
D

dalu.gelu

HI I have already define A in the begining of the thread, anyway A is a
class and here is the defn:
class A{
int x;
public: A(){ x=6;}
};

how to ensure that if i write overloaded assignment op and destructor
here then evrything will be fine...:)
 
L

LR

(e-mail address removed) wrote:

[Please do not top post, content moved]


> HI I have already define A in the begining of the thread, anyway A is a
> class and here is the defn:
> class A{
> int x;
> public: A(){ x=6;}
> };
>
> how to ensure that if i write overloaded assignment op and destructor
> here then evrything will be fine...:)

Depends on what you want them to do.



Heres one way.

class A {
int x;
public:
void swap(A &a) {
std::swap(a.x,x);
}
A() : x(6) {}
// note const A & in copy ctor
A(const A &a) : x(a.x) {}
A &operator=(const A &a) {
// see gotw for assigment ops
A temp(a);
swap(temp);
return *this;
}
};

class B {
std::auto_ptr<A> a1;
public:
void swap(B &b) {
// swap pointers, not instances
std::swap(a1,b.a1);
}
B() : a1() {}
B(const B &b) : a1( b.a1.get() ? new A(*b.a1.get()) : 0 ) {}
B &operator=(const B &b) {
B temp(b);
swap(temp);
return *this;
}
};


Now, if you have to use a raw pointer, then you can back out of this, by
replacing std::auto_ptr and creating your own destructor for class B,
where you will have to delete the thing the pointer is pointing to.
 
R

Ron Natalie

HI I have already define A in the begining of the thread, anyway A is a
class and here is the defn:
class A{
int x;
public: A(){ x=6;}
};

how to ensure that if i write overloaded assignment op and destructor
here then evrything will be fine...:)
You haven't answered the question of how B::a1 gets set to anything.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top