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