Click here to Skip to main content
15,896,269 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hi world
i have a question . please help me
is it possible to cast an object from parent class to an object to derived class??
i think it is not possible because derived class have attributes and methodes that may base class doesn't have them but i write the below code in visual studio and it show the unknown value.
class A
{
public:
	string x;
	A():x("kdf"){}
};
class B:public A
{
public:
	B(int i):y(i){}
	int y;
};
int _tmain(int argc, _TCHAR* argv[])
{
	A a;
	cout <<((B&)a).y;
	return 0;
}

but when i write
class A
{
public:
	string x;
	A():x("kdf"){}
};
class B:public A
{
public:
	B(int i):y(i){}
	int y;
};
int _tmain(int argc, _TCHAR* argv[])
{
	A a;
	cout <<((B)a).y;
	return 0;
}

i have an error but i don't khow why?
please say me why i should put & in the line
cout <<((B)a).y;

and why we can do this cast?
thank you very much
Posted
Comments
Stefan_Lang 20-Jun-14 2:31am    
In addition to CPallinis solution, I advise you to never use C style casts in a C++ prgram, and in fact avoid casts whenever you can. The only thing that forces you to use a cast is bad third party APIs that you cannot change (like MFC).

If you must cast a variable, use dynamic_cast, static_cast, etc.. Check ou tthis article for more information: http://www.cplusplus.com/doc/tutorial/typecasting/

You can do the cast because the C-like cast operator is just like a very big hammer and the compiler knows how to cast by reference (that means: interpret the address of an object like if it was the one of an object having different type).
On the other hand, you get the error (in the second scenario), because the compiler doesn't know how to convert an object of type A to an object of type B.
The bottom line is: use the C-like cast operator only if you know very well what you are doing.
 
Share this answer
 
Comments
Maciej Los 19-Jun-14 13:56pm    
+5!
Sergey Alexandrovich Kryukov 19-Jun-14 21:01pm    
I voted 4 this time. It's possible that OP needs to get the explanation that a compile-time (declared) type is not the same as a runtime type, so the case could be done like this only if the actual runtime type is compatible with the type to case to. C++ provides dynamic cast which can be used in safe way, but it has performance overhead.
—SA
Stefan_Lang 20-Jun-14 2:37am    
I've actually never read anyone arguing not to use dynamic_cast due to performance issues. My take on this is that the only real reason to use casts is a badly designed API that you cannot change, and if you have a performance bottleneck across an API, then your overall design is probably flawed!
Sergey Alexandrovich Kryukov 20-Jun-14 9:42am    
Of course; this is not an argument to use or not to use it, just a technical note.
Using dynamic cast is considered as the violation of pure OOP. However, OOP itself has intrinsic well-known design issues (sucha as circle ellipse problem), so dynamic cast is sometimes uses in designs I don't dare to call too bad. It's important to use any kind of cast at least sparingly, in rare cases.
—SA
Stefan_Lang 20-Jun-14 2:34am    
It's indeed a big hammer - it's the one used to hammer cubes into round holes!

+5 btw.
While CPallini already quite fittingly compared the C-style cast to a very big hammer, I'd like to add a different, but also very fitting comparison to the picture:

What you have actually done is take bike (an A), put a big label on it saying 'This is a tank!' (this is a B), and then you tried to run over the bus that was just passing by. Now guess who's gonna crash!

Just because you tell the compiler that your 'bike' is a 'tank', doesn't make it so. And it's really just that: a typecast is a way to tell the compiler: "Whatever you're thinking, I know better; this is a different type!". I've seen many programmers doing that while sitting on shabby bikes... X|
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900