Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Assinging a base object to an inherited object

Reply
Thread Tools

Assinging a base object to an inherited object

 
 
bg_ie@yahoo.com
Guest
Posts: n/a
 
      04-02-2007
Hi,

Lets say I have an class called Base which is inherited by a class
called Top. Now lets say I want to assign an object of type Base to an
object of type Top, how can I do this so that all the members present
in the Base object are assigned to the corresponding members in the
Top object, with the remaining values set to null?

Thanks for your help,

Barry.

 
Reply With Quote
 
 
 
 
Victor Bazarov
Guest
Posts: n/a
 
      04-02-2007
http://www.velocityreviews.com/forums/(E-Mail Removed) wrote:
> Lets say I have an class called Base which is inherited by a class
> called Top. Now lets say I want to assign an object of type Base to an
> object of type Top, how can I do this so that all the members present
> in the Base object are assigned to the corresponding members in the
> Top object, with the remaining values set to null?


Use a converting constructor

class Top : public Base {
int rest;
public:
Top(Base const& b) : Base(b), rest(0) {}
};

or a custom assignment operator

class Top : public Base {
int rest;
public:
Top& operator =(Base const& b) {
Base:perator=(b);
rest = 0;
return *this;
}
};

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask


 
Reply With Quote
 
 
 
 
Chris Theis
Guest
Posts: n/a
 
      04-02-2007

<(E-Mail Removed)> wrote in message
news:(E-Mail Removed) oups.com...
> Hi,
>
> Lets say I have an class called Base which is inherited by a class
> called Top. Now lets say I want to assign an object of type Base to an
> object of type Top, how can I do this so that all the members present
> in the Base object are assigned to the corresponding members in the
> Top object, with the remaining values set to null?
>


You could implement a custom assignment op or conversion ctor that takes a
Base object and calls the base class' assignment op after upcasting the this
pointer to base, or calling it explicitely.

Of course you'd have to set the remaining member variables of Top to zero
manually but usually that would be taken care of by the initial constructor
anyway (if there is no reason to set some variables to a predefined state
that is not valid anymore after the assignment of a base object).

Cheers
Chris


 
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
assinging an extension for incoming fxo line aung.naingoo@gmail.com Cisco 1 02-09-2006 11:56 AM
Object creation - Do we really need to create a parent for a derieved object - can't the base object just point to an already created base object jon wayne C++ 9 09-22-2005 02:06 AM
assinging strings to two dimensional array Reginald Carlier C++ 1 06-10-2004 01:19 PM
Re-assinging partitions Mike Computer Support 1 01-20-2004 10:00 AM
Assinging dynamic content from file to textbox Greg ASP .Net 0 12-18-2003 10:10 PM



Advertisments