Re: derived class copy constructor...

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

  1. ES Kim

    ES Kim Guest

    "john smith" <> wrote in message
    news:bgcipc$rmg$...
    > Hi,
    >
    > I have a question about initializing the base class in a copy constructor.
    > So here is my example:
    >
    > class base {
    > public:
    > // some methods;
    > private:
    > vector<int> a;
    > list<int> b;
    > };
    > class derived : public base {
    > public:
    > derived(const derived& x) {
    > p = new char[x.getsize()];
    > // copy p;
    > // but what about the base class?
    > }
    > private:
    > char* p;
    > };
    >
    > In the copy constructor in the derived, how do I initialize the contents of
    > the base class? There are no access functions, no constructor that
    > initializes each member, and no overloaded operator= (but I'm not sure even
    > that would help). Is it simply not possible without some modification to
    > the base class?
    >
    > thanks in advance.
    > smith
    >
    >


    You should define copy constructor for the base class, and then

    derived(const derived& x) : base(x) {
    p = new char[x.getsize()];
    // copy p;
    }

    --
    ES Kim
    ES Kim, Aug 1, 2003
    #1
    1. Advertising

  2. ES Kim

    ES Kim Guest

    "ES Kim" <> wrote in message
    news:bgch8l$rht$...
    > "john smith" <> wrote in message
    > news:bgcipc$rmg$...
    > > Hi,
    > >
    > > I have a question about initializing the base class in a copy constructor.
    > > So here is my example:
    > >
    > > class base {
    > > public:
    > > // some methods;
    > > private:
    > > vector<int> a;
    > > list<int> b;
    > > };
    > > class derived : public base {
    > > public:
    > > derived(const derived& x) {
    > > p = new char[x.getsize()];
    > > // copy p;
    > > // but what about the base class?
    > > }
    > > private:
    > > char* p;
    > > };
    > >
    > > In the copy constructor in the derived, how do I initialize the contents of
    > > the base class? There are no access functions, no constructor that
    > > initializes each member, and no overloaded operator= (but I'm not sure even
    > > that would help). Is it simply not possible without some modification to
    > > the base class?
    > >
    > > thanks in advance.
    > > smith
    > >
    > >

    >
    > You should define copy constructor for the base class, and then
    >
    > derived(const derived& x) : base(x) {
    > p = new char[x.getsize()];
    > // copy p;
    > }
    >


    Sorry, You don't have to define the copy constructor explicitly
    in this case.

    --
    ES Kim
    ES Kim, Aug 1, 2003
    #2
    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:
    584
    Larry I Smith
    May 18, 2005
  2. ali
    Replies:
    4
    Views:
    559
    David Harmon
    Mar 5, 2007
  3. Replies:
    1
    Views:
    385
    myork
    May 23, 2007
  4. Replies:
    1
    Views:
    374
    Victor Bazarov
    May 23, 2007
  5. Rahul
    Replies:
    16
    Views:
    1,177
    terminator
    Nov 7, 2007
Loading...

Share This Page