why can not we assign base class object reference to derived class pointer?
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;
}
Re: why can not we assign base class object reference to derived class pointer?
How are Base and Derived related? You might want to compile your sample code to avoid confusion. Oh, and place them between [code] and [/code] forum bbcode tags.
Re: why can not we assign base class object reference to derived class pointer?
that is because base class does not know about the derived class attributes and methods.
Re: why can not we assign base class object reference to derived class pointer?
Code:
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.
}
Re: why can we assign base class object reference to derived class pointer?
Derived is derived from Base;
class Derived : public Base
{
---------
---------
};
void main()
{
Base bo;
Derived *pd = &bo; // why is this wrong.
}
Please lemme know this
Re: why can we assign base class object reference to derived class pointer?
Quote:
Originally Posted by adapanaidu
Code:
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
Re: why can we assign base class object reference to derived class pointer?
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.
Code:
Derived *pd = dynamic_cast<Derived*>(&bo);
Re: why can we assign base class object reference to derived class pointer?
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.
Re: why can we assign base class object reference to derived class pointer?
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.