Re: derived class copy constructor...

Discussion in 'C++' started by Novice, Aug 1, 2003.

  1. Novice

    Novice Guest

    "Novice" <> wrote in message
    news:bgcjd1$5bf$...
    | "john smith" <> wrote in message
    | news:bgcipc$rmg$...
    |
    | 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
    Novice, Aug 1, 2003
    #1
    1. Advertising

  2. Novice

    ES Kim Guest

    "Novice" <> wrote in message
    news:bgcjk5$5ff$...
    >
    > 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
    ES Kim, Aug 1, 2003
    #2
    1. Advertising

  3. "ES Kim" <> wrote in message
    news:bgcj4i$sn1$...
    > "Novice" <> wrote in message
    > news:bgcjk5$5ff$...
    > >
    > > 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
    John Harrison, Aug 1, 2003
    #3
  4. Novice

    john smith Guest

    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" <> wrote in message
    news:bgcj4i$sn1$...
    > "Novice" <> wrote in message
    > news:bgcjk5$5ff$...
    > >
    > > 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
    >
    >
    john smith, Aug 1, 2003
    #4
  5. Novice

    ES Kim Guest

    "john smith" <> wrote in message
    news:bgd6rk$13a4$...
    > 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::eek:perator=(x); // assign the base class portion
    delete[] p;
    p = new char[x.getsize()];
    // copy p;
    return *this;
    }

    --
    ES Kim
    ES Kim, Aug 1, 2003
    #5
  6. 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::eek: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
    Karl Heinz Buchegger, Aug 1, 2003
    #6
  7. Novice

    john smith Guest

    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" <> wrote in message
    news:...
    >
    >
    > 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::eek: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
    >
    john smith, Aug 1, 2003
    #7
  8. Novice

    john smith Guest

    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" <> wrote in message
    news:bgd72k$dl4$...
    > "john smith" <> wrote in message
    > news:bgd6rk$13a4$...
    > > 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::eek:perator=(x); // assign the base class portion
    > delete[] p;
    > p = new char[x.getsize()];
    > // copy p;
    > return *this;
    > }
    >
    > --
    > ES Kim
    >
    >
    john smith, Aug 1, 2003
    #8
  9. Novice

    john smith Guest

    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" <> wrote in message
    news:bgdff7$nevkq$-berlin.de...
    >
    > "john smith" <> wrote in message
    > news:bgdf41$15sp$...
    > > 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
    >
    >
    john smith, Aug 1, 2003
    #9
  10. Novice

    ES Kim Guest

    "john smith" <> wrote in message
    news:bgdguh$16ig$...
    > 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
    ES Kim, Aug 1, 2003
    #10
  11. "john smith" <> wrote in message
    news:bgdguh$16ig$...
    > 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.
    >


    Rule of three (which is a stupid name) is that if you class has one of copy
    ctor, assign op or dtor then it probably needs all three.

    > That's great.
    >
    > I love this language!
    >


    It all hangs together if you understand it, but too many people don't (where
    would c.l.c++ be without them though).

    john
    John Harrison, Aug 1, 2003
    #11
  12. Novice

    Stephen Howe Guest

    "john smith" <> wrote in message
    news:bgdguh$16ig$...
    > 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.


    Assigment operator is special just like constructor,copy constructor and
    destructor.
    If you don;t define them the compiler will synthesis them for you.

    It is because of backward compatibility with C.

    Given (talking C here)

    struct structtag
    { int mem;
    };

    struct structtag o1;
    struct structtag o2;

    you can do

    o1 = o2; // which is where assignment operator come in. You did not have to
    write the code that copies o2 to o1, the compiler automatically generated it
    for you, therefore since C++ supports this it must do the same.

    The other automatically generated operators are the address operator and
    const address operator as you can do

    struct structtag *p = &o1;

    Stephen Howe

    >
    > That's great.
    >
    > I love this language!
    >
    > s
    > "John Harrison" <> wrote in message
    > news:bgdff7$nevkq$-berlin.de...
    > >
    > > "john smith" <> wrote in message
    > > news:bgdf41$15sp$...
    > > > 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
    > >
    > >

    >
    >
    Stephen Howe, Aug 1, 2003
    #12
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. mrstephengross
    Replies:
    5
    Views:
    593
    Larry I Smith
    May 18, 2005
  2. ali
    Replies:
    4
    Views:
    568
    David Harmon
    Mar 5, 2007
  3. Replies:
    1
    Views:
    393
    myork
    May 23, 2007
  4. Replies:
    1
    Views:
    384
    Victor Bazarov
    May 23, 2007
  5. Rahul
    Replies:
    16
    Views:
    1,194
    terminator
    Nov 7, 2007
Loading...

Share This Page