Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Re: derived class copy constructor...

Reply
Thread Tools

Re: derived class copy constructor...

 
 
Novice
Guest
Posts: n/a
 
      08-01-2003

"Novice" <(E-Mail Removed)> wrote in message
news:bgcjd1$5bf$(E-Mail Removed)...
| "john smith" <(E-Mail Removed)> wrote in message
| news:bgcipc$rmg$(E-Mail Removed)...
|
| Try this:
|
| | derived(const derived& x) :base(x){
|

Oops... in my attempt at brevity I missed the fact you hadn't actually
written a copy constructor for your base class

You also need to add this in your base class:
class base {
public:
// some methods;
base (const base & base_):a(base_.a), b(base_.b){
}
private:
vector<int> a;
list<int> b;
};

sorry.. though I'm sure I will get responses to my previous brief post
cutting me up... ah well,
Novice


 
Reply With Quote
 
 
 
 
ES Kim
Guest
Posts: n/a
 
      08-01-2003
"Novice" <(E-Mail Removed)> wrote in message
news:bgcjk5$5ff$(E-Mail Removed)...
>
> Oops... in my attempt at brevity I missed the fact you hadn't actually
> written a copy constructor for your base class
>
> You also need to add this in your base class:
> class base {
> public:
> // some methods;
> base (const base & base_):a(base_.a), b(base_.b){
> }
> private:
> vector<int> a;
> list<int> b;
> };
>
> sorry.. though I'm sure I will get responses to my previous brief post
> cutting me up... ah well,
> Novice
>
>


That is what the default copy constructor of the base class does,
so you don't have to define it.
Don't take my word as offensive, I made the opposite mistake myself.

--
ES Kim


 
Reply With Quote
 
 
 
 
John Harrison
Guest
Posts: n/a
 
      08-01-2003

"ES Kim" <(E-Mail Removed)> wrote in message
news:bgcj4i$sn1$(E-Mail Removed)...
> "Novice" <(E-Mail Removed)> wrote in message
> news:bgcjk5$5ff$(E-Mail Removed)...
> >
> > Oops... in my attempt at brevity I missed the fact you hadn't actually
> > written a copy constructor for your base class
> >
> > You also need to add this in your base class:
> > class base {
> > public:
> > // some methods;
> > base (const base & base_):a(base_.a), b(base_.b){
> > }
> > private:
> > vector<int> a;
> > list<int> b;
> > };
> >
> > sorry.. though I'm sure I will get responses to my previous brief post
> > cutting me up... ah well,
> > Novice
> >
> >

>
> That is what the default copy constructor of the base class does,
> so you don't have to define it.
> Don't take my word as offensive, I made the opposite mistake myself.
>
> --
> ES Kim
>


It would make C++ very inflexible if you did. Imagine, every class would
have to define a copy constructor, just in case someone later decided to
derive another class from it.

john


 
Reply With Quote
 
john smith
Guest
Posts: n/a
 
      08-01-2003
Thanks guys... That really helped.

Now, given the same classes, is it possible to define operator= for the
derived class?

I think the derived class for operator= has to return *this, correct? But
now with a copy constructor, I can make a new instance of derived, but I
can't assign that to *this, because I'm trying to define operator=... see my
problem? Any help would be appreciated again... thanks.

Smith

"ES Kim" <(E-Mail Removed)> wrote in message
news:bgcj4i$sn1$(E-Mail Removed)...
> "Novice" <(E-Mail Removed)> wrote in message
> news:bgcjk5$5ff$(E-Mail Removed)...
> >
> > Oops... in my attempt at brevity I missed the fact you hadn't actually
> > written a copy constructor for your base class
> >
> > You also need to add this in your base class:
> > class base {
> > public:
> > // some methods;
> > base (const base & base_):a(base_.a), b(base_.b){
> > }
> > private:
> > vector<int> a;
> > list<int> b;
> > };
> >
> > sorry.. though I'm sure I will get responses to my previous brief post
> > cutting me up... ah well,
> > Novice
> >
> >

>
> That is what the default copy constructor of the base class does,
> so you don't have to define it.
> Don't take my word as offensive, I made the opposite mistake myself.
>
> --
> ES Kim
>
>



 
Reply With Quote
 
ES Kim
Guest
Posts: n/a
 
      08-01-2003
"john smith" <(E-Mail Removed)> wrote in message
news:bgd6rk$13a4$(E-Mail Removed)...
> Thanks guys... That really helped.
>
> Now, given the same classes, is it possible to define operator= for the
> derived class?
>
> I think the derived class for operator= has to return *this, correct? But
> now with a copy constructor, I can make a new instance of derived, but I
> can't assign that to *this, because I'm trying to define operator=... see my
> problem? Any help would be appreciated again... thanks.
>


derived& operator=(const derived& x)
{
if (this == &x) return *this;
base:perator=(x); // assign the base class portion
delete[] p;
p = new char[x.getsize()];
// copy p;
return *this;
}

--
ES Kim


 
Reply With Quote
 
Karl Heinz Buchegger
Guest
Posts: n/a
 
      08-01-2003


john smith wrote:
>
> Thanks guys... That really helped.
>
> Now, given the same classes, is it possible to define operator= for the
> derived class?
>
> I think the derived class for operator= has to return *this, correct? But
> now with a copy constructor, I can make a new instance of derived, but I
> can't assign that to *this, because I'm trying to define operator=... see my
> problem? Any help would be appreciated again... thanks.
>


So your goal is to use the copy ctor in the assignment operator?

You can do that, and the way to do it is:

derived& derived:perator=( const derived& rhs )
{
derived tmp( rhs ); // create a copy
Swap( *this, tmp ); // swap the content of this object
// with the content of the copy
return *this;
}

Of yourse you now need to define a swap function, which swaps the
internals of 2 objects.

But the question is: why go through all this hazzle?
In your example the base class contains a vector and
a list object. No special handling in the cctor, assignment op
and destructor is needed. In the derived class you introduced
a character pointer, which seems to be used like a string.
So I wonder: why don't you use std::string in the first place.
It would free you from all this problems, just like vector
or list have freed you from problems in the base class.

class base {
public:
// some methods;
private:
vector<int> a;
list<int> b;
};
class derived : public base {
public:

private:
string p;
};

Look Ma, no copy constructor needed!

--
Karl Heinz Buchegger
http://www.velocityreviews.com/forums/(E-Mail Removed)
 
Reply With Quote
 
john smith
Guest
Posts: n/a
 
      08-01-2003
right, thanks for your help. Obviously the answer to your questions is that
those examples are just trivial. The actual code is not that trivial

smith
"Karl Heinz Buchegger" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
>
>
> john smith wrote:
> >
> > Thanks guys... That really helped.
> >
> > Now, given the same classes, is it possible to define operator= for the
> > derived class?
> >
> > I think the derived class for operator= has to return *this, correct?

But
> > now with a copy constructor, I can make a new instance of derived, but I
> > can't assign that to *this, because I'm trying to define operator=...

see my
> > problem? Any help would be appreciated again... thanks.
> >

>
> So your goal is to use the copy ctor in the assignment operator?
>
> You can do that, and the way to do it is:
>
> derived& derived:perator=( const derived& rhs )
> {
> derived tmp( rhs ); // create a copy
> Swap( *this, tmp ); // swap the content of this object
> // with the content of the copy
> return *this;
> }
>
> Of yourse you now need to define a swap function, which swaps the
> internals of 2 objects.
>
> But the question is: why go through all this hazzle?
> In your example the base class contains a vector and
> a list object. No special handling in the cctor, assignment op
> and destructor is needed. In the derived class you introduced
> a character pointer, which seems to be used like a string.
> So I wonder: why don't you use std::string in the first place.
> It would free you from all this problems, just like vector
> or list have freed you from problems in the base class.
>
> class base {
> public:
> // some methods;
> private:
> vector<int> a;
> list<int> b;
> };
> class derived : public base {
> public:
>
> private:
> string p;
> };
>
> Look Ma, no copy constructor needed!
>
> --
> Karl Heinz Buchegger
> (E-Mail Removed)



 
Reply With Quote
 
john smith
Guest
Posts: n/a
 
      08-01-2003
Is there a way of doing this without calling operator= on the base? In my
example there is no operator= defined in the base. Thanks

s
"ES Kim" <(E-Mail Removed)> wrote in message
news:bgd72k$dl4$(E-Mail Removed)...
> "john smith" <(E-Mail Removed)> wrote in message
> news:bgd6rk$13a4$(E-Mail Removed)...
> > Thanks guys... That really helped.
> >
> > Now, given the same classes, is it possible to define operator= for the
> > derived class?
> >
> > I think the derived class for operator= has to return *this, correct?

But
> > now with a copy constructor, I can make a new instance of derived, but I
> > can't assign that to *this, because I'm trying to define operator=...

see my
> > problem? Any help would be appreciated again... thanks.
> >

>
> derived& operator=(const derived& x)
> {
> if (this == &x) return *this;
> base:perator=(x); // assign the base class portion
> delete[] p;
> p = new char[x.getsize()];
> // copy p;
> return *this;
> }
>
> --
> ES Kim
>
>



 
Reply With Quote
 
john smith
Guest
Posts: n/a
 
      08-01-2003
yes, I just wrote a simple program to verify this. Surprising, really. I
always thought that the 3 things you get for free for any class from the
compiler are: constructor, copy constructor, and destructor. Like that's
the rule of 3 or something. I didn't know operator= was free too.

That's great.

I love this language!

s
"John Harrison" <(E-Mail Removed)> wrote in message
news:bgdff7$nevkq$(E-Mail Removed)-berlin.de...
>
> "john smith" <(E-Mail Removed)> wrote in message
> news:bgdf41$15sp$(E-Mail Removed)...
> > Is there a way of doing this without calling operator= on the base? In

my
> > example there is no operator= defined in the base. Thanks
> >

>
> But there doesn't need to be. Exactly the same situation as with the copy
> constructor. You can call the base operator= even though you haven't

defined
> it.
>
> john
>
>



 
Reply With Quote
 
ES Kim
Guest
Posts: n/a
 
      08-01-2003
"john smith" <(E-Mail Removed)> wrote in message
news:bgdguh$16ig$(E-Mail Removed)...
> yes, I just wrote a simple program to verify this. Surprising, really. I
> always thought that the 3 things you get for free for any class from the
> compiler are: constructor, copy constructor, and destructor. Like that's
> the rule of 3 or something. I didn't know operator= was free too.
>
> That's great.
>
> I love this language!
>


Yeah, I'm just grateful to all the C++ people
considering doing-all-the-chores days in C.

--
ES Kim


 
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
Derived Structure in Derived Class?? David C++ 3 01-29-2008 07:38 AM
Derived::Derived(const Base&) and Derived& operator=(const Base&) developereo@hotmail.com C++ 1 05-23-2007 01:44 PM
Derived::Derived(const Base&) and Derived& operator=(const Base&) developereo@hotmail.com C++ 1 05-23-2007 12:07 AM
Re: Copy assignment for derived class when base class has privatemembers Karl Heinz Buchegger C++ 3 08-06-2003 11:44 AM
Re: Copy assignment for derived class when base class has private members John Harrison C++ 0 08-06-2003 08:50 AM



Advertisments