Simple C++ Question About Inheritance

Started by
3 comments, last by Whirlwind 23 years, 11 months ago
If I declare parent object as part of a child object variable list, can I simply assign any child object to the parent object? Example: class parentobject { public: some variables; somefunctions(); } class childobject1:parentobject { public: parentobject thingie; etc... } class childobject2:parentobject { blah blah blah } . . . // main code childobject1 child1; childobject2 child2; child1.thingie=child2; Honest, I''m trying.
Advertisement
Yeah, that should work fine.
You forgot something. When you use inheritance, you have to use either public or protected when deriving a class like this:

class CClass
{
public:
int x;
};

class DClass: public CClass
{
public:
int y;
};

If you code it, they will come...

Commander M
http://commanderm.8m.com
cmndrm@commanderm.8m.com
Actually:

child1.thingie = child2;

Won''t work unless you either:

a) make child1.thingie a pointer or a reference.
b) overload operator= to assign a childobject2 to a parentobject.

Mason McCuskey
Spin Studios - home of Quaternion, 2000 GDC Indie Games Fest Finalist!
www.spin-studios.com
Founder, Cuttlefish Industries
The Cuttlefish Engine lets anyone develop great games for iPad, iPhone, Android, WP7, the web, and more!
Oh yeah, forgot about that ''public, private, friend, protected'' for inheritance.

I kind of thought that simply assigning a object to it''s parent was too simple. It probably will be a pointer in a linked list by the time it is all said and done with.

Thanks a lot, all. It has been a year since I''ve had a go at C++, C worked fine form my <100 line utility coding I was doing. Time break out the cliff notes .

This topic is closed to new replies.

Advertisement