Velocity Reviews - Computer Hardware Reviews

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

Reply
Thread Tools

Re: derived class copy constructor...

 
 
ES Kim
Guest
Posts: n/a
 
      08-01-2003
"john smith" <(E-Mail Removed)> wrote in message
news:bgcipc$rmg$(E-Mail Removed)...
> 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


 
Reply With Quote
 
 
 
 
ES Kim
Guest
Posts: n/a
 
      08-01-2003
"ES Kim" <(E-Mail Removed)> wrote in message
news:bgch8l$rht$(E-Mail Removed)...
> "john smith" <(E-Mail Removed)> wrote in message
> news:bgcipc$rmg$(E-Mail Removed)...
> > 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


 
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