|
-
May 10th, 2006, 04:56 AM
#1
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;
}
Last edited by adapanaidu; May 10th, 2006 at 05:02 AM.
Reason: question is wrong.
-
May 10th, 2006, 05:07 AM
#2
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.
-
May 10th, 2006, 05:37 AM
#3
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.
-
May 10th, 2006, 05:53 AM
#4
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.
}
-
May 10th, 2006, 06:40 AM
#5
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
-
May 10th, 2006, 06:54 AM
#6
Re: why can we assign base class object reference to derived class pointer?
 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
-
May 10th, 2006, 06:57 AM
#7
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);
-
May 10th, 2006, 10:46 AM
#8
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.
-
May 10th, 2006, 02:18 PM
#9
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.
Correct is better than fast. Simple is better than complex. Clear is better than cute. Safe is better than insecure.
-- Sutter and Alexandrescu, C++ Coding Standards
Programs must be written for people to read, and only incidentally for machines to execute.
-- Harold Abelson and Gerald Jay Sussman
The cheapest, fastest and most reliable components of a computer system are those that aren't there.
-- Gordon Bell
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|