N
nas
Consider this example program which i hav taken from some site
#include <iostream>
using namespace std;
class Base1 {
public:
virtual void f() { }
};
class Base2 {
public:
virtual void f() { }
};
class Base3 {
public:
virtual void f() { }
};
class Drive : public Base1, public Base2, public Base3 {
};
// any non zero value because multiply zero with any no is zero
#define SOME_VALUE 1
int main() {
cout << (DWORD)static_cast<Base1*>((Drive*)SOME_VALUE)-SOME_VALUE <<
endl;
cout << (DWORD)static_cast<Base2*>((Drive*)SOME_VALUE)-SOME_VALUE <<
endl;
cout << (DWORD)static_cast<Base3*>((Drive*)SOME_VALUE)-SOME_VALUE <<
endl;
return 0;
}
Output of this prgram is:
0
4
8
Can any one tell me How does
(DWORD)static_cast<Base1*>((Drive*)SOME_VALUE)-SOME_VALUE
(DWORD)static_cast<Base2*>((Drive*)SOME_VALUE)-SOME_VALUE
(DWORD)static_cast<Base3*>((Drive*)SOME_VALUE)-SOME_VALUE
Gives you 0,4,8?
More surprisingly if i define SOME_VALUE to 0, that is if i do
#define SOME_VALUE 0
I get output as 0,0,0 itself! if i assign ANY non zero number to
SOME_VALUE then i wil get the first answer which is 0,4,8.
Can any one exaplain me how?
#include <iostream>
using namespace std;
class Base1 {
public:
virtual void f() { }
};
class Base2 {
public:
virtual void f() { }
};
class Base3 {
public:
virtual void f() { }
};
class Drive : public Base1, public Base2, public Base3 {
};
// any non zero value because multiply zero with any no is zero
#define SOME_VALUE 1
int main() {
cout << (DWORD)static_cast<Base1*>((Drive*)SOME_VALUE)-SOME_VALUE <<
endl;
cout << (DWORD)static_cast<Base2*>((Drive*)SOME_VALUE)-SOME_VALUE <<
endl;
cout << (DWORD)static_cast<Base3*>((Drive*)SOME_VALUE)-SOME_VALUE <<
endl;
return 0;
}
Output of this prgram is:
0
4
8
Can any one tell me How does
(DWORD)static_cast<Base1*>((Drive*)SOME_VALUE)-SOME_VALUE
(DWORD)static_cast<Base2*>((Drive*)SOME_VALUE)-SOME_VALUE
(DWORD)static_cast<Base3*>((Drive*)SOME_VALUE)-SOME_VALUE
Gives you 0,4,8?
More surprisingly if i define SOME_VALUE to 0, that is if i do
#define SOME_VALUE 0
I get output as 0,0,0 itself! if i assign ANY non zero number to
SOME_VALUE then i wil get the first answer which is 0,4,8.
Can any one exaplain me how?