Click to See Complete Forum and Search --> : why can we assign base class object reference to derived class pointer?
adapanaidu
May 10th, 2006, 04:56 AM
class Base
{
public:
Base(int ii = 0 , char cc = 65) : i(ii) , c(cc) {}
int i;
char c;
};
class Derived
{
public:
Derived(int ii = 0 , char cc = 65 , int id = 10 , char cd = 66) : Base(ii , cc) , i(id) , c(cd) {}
int i;
char c;
};
void main()
{
Base bo;
Derived *dp = &bo;
}
laserlight
May 10th, 2006, 05:07 AM
How are Base and Derived related? You might want to compile your sample code to avoid confusion. Oh, and place them between and forum bbcode tags.
luh@r
May 10th, 2006, 05:37 AM
that is because base class does not know about the derived class attributes and methods.
NMTop40
May 10th, 2006, 05:53 AM
class Base
{
public:
Base(int ii = 0 , char cc = 65) : i(ii) , c(cc) {}
int i;
char c;
// attributes should preferably be private
};
class Derived
{
public:
Derived(int ii = 0 , char cc = 65 , int id = 10 , char cd = 66)
: Base(ii , cc) , i(id) , c(cd) {}
// but Derived doesn't derive from Base. Did you mean:
// class Derived : public Base ?
int i;
char c;
// these "hide" the variables with the same name in the base class, except
// that Base isn't a base class, and that they shouldn't have been public
// there in the first place.
};
void main() // oh no, not that again.
{
Base bo;
Derived *dp = &bo;
// needs reinterpret_cast.
}
adapanaidu
May 10th, 2006, 06:40 AM
Derived is derived from Base;
class Derived : public Base
{
---------
---------
};
void main()
{
Base bo;
Derived *pd = &bo; // why is this wrong.
}
Please lemme know this
wildfrog
May 10th, 2006, 06:54 AM
Base bo;
Derived *pd = &bo; // why is this wrong.
Because bo is a instance of Base, not an instance of Derived. In other words your're trying to cast it into something it's not.
- petter
laserlight
May 10th, 2006, 06:57 AM
why is this wrong
Derived is-a Base, but not vice versa. Though if Base has at least one virtual function, under some circumstances a dynamic_cast might be used, e.g.
Derived *pd = dynamic_cast<Derived*>(&bo);
NMTop40
May 10th, 2006, 10:46 AM
dynamic_cast should generally be avoided.
It is fine in an object-broking system. Otherwise don't use it.
I use my own object-broking system which works in C++ only. It does not force you to derive your classes from a common base class. It does force you to create a builder for your object and the builder derives from a common base class. There are loads of templates to build most classes you would want. Your class can derive from as many classes as it likes, but the builder can only support one interface, a problem I haven't yet found a solution to
(that Builder<Derived> and Builder<Base> are unrelated).
I'm a bit O/T now but basically trying to explain that you should not generally cast down the hierarchy, but there are sometimes when it's necessary. In an object-broking system you load them dynamically at run-time from a string, and you have to be able to check that you were given an object of the type you really want.
Graham
May 10th, 2006, 02:18 PM
In the example given in the OP, dynamic_cast will not work, no matter how many virtual functions it has. A Base is-not-a derived, in the same way that an Animal is-not-a Dog. (But a Dog is-a Animal.
codeguru.com
Copyright Internet.com Inc., All Rights Reserved.