Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > define a copy constructor in a class having data member as an object of another class

Reply
Thread Tools

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

 
 
dalu.gelu@gmail.com
Guest
Posts: n/a
 
      11-09-2006
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

 
Reply With Quote
 
 
 
 
VJ
Guest
Posts: n/a
 
      11-09-2006
http://www.velocityreviews.com/forums/(E-Mail Removed) wrote:
> 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
 
Reply With Quote
 
 
 
 
Greg
Guest
Posts: n/a
 
      11-09-2006
(E-Mail Removed) wrote:
> 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

 
Reply With Quote
 
Greg
Guest
Posts: n/a
 
      11-09-2006
(E-Mail Removed) wrote:
> 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

 
Reply With Quote
 
dalu.gelu@gmail.com
Guest
Posts: n/a
 
      11-09-2006
thanks,

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

 
Reply With Quote
 
Greg
Guest
Posts: n/a
 
      11-09-2006
(E-Mail Removed) wrote:
> 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

 
Reply With Quote
 
Greg
Guest
Posts: n/a
 
      11-09-2006
(E-Mail Removed) wrote:
> 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

 
Reply With Quote
 
dalu.gelu@gmail.com
Guest
Posts: n/a
 
      11-09-2006
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.

 
Reply With Quote
 
Ron Natalie
Guest
Posts: n/a
 
      11-09-2006
(E-Mail Removed) wrote:
> 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.
>

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.

 
Reply With Quote
 
dalu.gelu@gmail.com
Guest
Posts: n/a
 
      11-09-2006
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...

Ron Natalie wrote:
> (E-Mail Removed) wrote:
> > 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.
> >

> 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.


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
template copy constructor vs normal copy constructor cinsk C++ 35 10-10-2010 11:14 PM
A constructor calling another constructor (default constructor)? Generic Usenet Account C++ 10 11-28-2007 04:12 AM
Calling base class constructor from derived class Copy constructor ali C++ 4 03-05-2007 09:15 AM
Class with no default constructor as a member in another class? Henrik Goldman C++ 4 09-09-2006 02:27 PM
deep/shallow copy - constructor v Object.copy() VisionSet Java 8 04-29-2004 10:41 PM



Advertisments