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

 
 
LR
Guest
Posts: n/a
 
      11-09-2006
http://www.velocityreviews.com/forums/(E-Mail Removed) wrote:

[Please do not top post, content moved]



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

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