Getting the hierarchy wrong
-
Here it comes :
class A
{
public:
virtual void run() = 0;
};class B :
public class A,
public SomeOtherClass
{
public:
virtual void run();
}That's in the headers. Now an excerpt from main :
SomeOtherClass *dummy = new B();
reinterpret_cast<A *>(dummy)->run();
This ended up calling some other function from SomeOtherClass. Did'nt take too long to find, but i still think it's tricky, cause of the strange way it showed.
-
Here it comes :
class A
{
public:
virtual void run() = 0;
};class B :
public class A,
public SomeOtherClass
{
public:
virtual void run();
}That's in the headers. Now an excerpt from main :
SomeOtherClass *dummy = new B();
reinterpret_cast<A *>(dummy)->run();
This ended up calling some other function from SomeOtherClass. Did'nt take too long to find, but i still think it's tricky, cause of the strange way it showed.
reinterpret_cast
is the cast of absolute last resort, and it simply converts the raw bits from one format to another. In this case it will simply call the function inSomeOtherClass
that occupies the same position in the vtable thatrun
does inA
(in your example, the first one). Because you need to cast down the hierarchy and you don't know whether the most-derived class thatdummy
actually points to derives fromA
, you need to use adynamic_cast
. The fact thatdummy
points to aB
is lost immediately after the assignment occurs.Stability. What an interesting concept. -- Chris Maunder
-
Here it comes :
class A
{
public:
virtual void run() = 0;
};class B :
public class A,
public SomeOtherClass
{
public:
virtual void run();
}That's in the headers. Now an excerpt from main :
SomeOtherClass *dummy = new B();
reinterpret_cast<A *>(dummy)->run();
This ended up calling some other function from SomeOtherClass. Did'nt take too long to find, but i still think it's tricky, cause of the strange way it showed.
That's a cross-cast, no? Wouldn't you need to cast from
SomeOtherClass*
toB*
and then toA*
?--Mike-- Visual C++ MVP :cool: LINKS~! Ericahist | PimpFish | CP SearchBar v3.0 | C++ Forum FAQ
-
Here it comes :
class A
{
public:
virtual void run() = 0;
};class B :
public class A,
public SomeOtherClass
{
public:
virtual void run();
}That's in the headers. Now an excerpt from main :
SomeOtherClass *dummy = new B();
reinterpret_cast<A *>(dummy)->run();
This ended up calling some other function from SomeOtherClass. Did'nt take too long to find, but i still think it's tricky, cause of the strange way it showed.
This is part of the reason why multiple inheritance should be avoided.
If you decide to become a software engineer, you are signing up to have a 1/2" piece of silicon tell you exactly how stupid you really are for 8 hours a day, 5 days a week Zac
-
This is part of the reason why multiple inheritance should be avoided.
If you decide to become a software engineer, you are signing up to have a 1/2" piece of silicon tell you exactly how stupid you really are for 8 hours a day, 5 days a week Zac
Sorry have to reply to your sig... You only work half time?
-
Sorry have to reply to your sig... You only work half time?
:laugh: Well, guess I should add "at a minimum" to that. Though, where I work now isn't nearly as bad as where I use to work. I work about 41 hours a week instead of 70 ... and I get paid more.
If you decide to become a software engineer, you are signing up to have a 1/2" piece of silicon tell you exactly how stupid you really are for 8 hours a day, 5 days a week Zac
-
:laugh: Well, guess I should add "at a minimum" to that. Though, where I work now isn't nearly as bad as where I use to work. I work about 41 hours a week instead of 70 ... and I get paid more.
If you decide to become a software engineer, you are signing up to have a 1/2" piece of silicon tell you exactly how stupid you really are for 8 hours a day, 5 days a week Zac
I took a job during the .com bubble and calculated my hourly wage the first month as less than minimum wage. I look for much more stable work environments lately. The place I'm at now is pretty good. But we digress. Scott