On 2 May 2004 11:45:11 -0700,
sr********@lycos.com (srikanth) wrote:
#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
Well, that value isn't really being multiplied; I suspect the results you
get by setting SOME_VALUE to 0 has more to do with how NULL pointers are
special-cased (or produce undefined behavior, perhaps) than anything
relating to multiplication by zero. And if you try substituting 2, or 3,
etc., for SOME_VALUE, you'll see it increases the results by that much for
each pointer, and won't multiply it.
#define SOME_VALUE 1
int main()
{
cout << (int)static_cast<Base1*>((Drive*)SOME_VALUE)-SOME_VALUE <<
endl;
cout << (int)static_cast<Base2*>((Drive*)SOME_VALUE)-SOME_VALUE <<
endl;
cout << (int)static_cast<Base3*>((Drive*)SOME_VALUE)-SOME_VALUE <<
endl;
return 0;
}
output is
0
4
8
the memory size of each virtual pointer but i cant understood how the
statements in cout calculated it
Not "memory size", /offset/. The cast is going to offset the value you're
casting (presumed to point to the beginning of a Drive object) by the
amount necessary to point to the appropriate sub-object part you're casting
to (Base1, Base2 or Base3). To get there, it ends up having to "pass over"
the "previous" sub-objects. Each subobject has a vtbl for its one virtual
function; that accounts for the 4-bytes each occupies that must be passed
over.
-leor
--
Leor Zolman --- BD Software ---
www.bdsoft.com
On-Site Training in C/C++, Java, Perl and Unix
C++ users: download BD Software's free STL Error Message Decryptor at:
www.bdsoft.com/tools/stlfilt.html