 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
Bob Guest
|
Posted: Sat Oct 30, 2004 7:48 am Post subject: why size of *this and size of basic and derived class differ |
|
|
Try the following code
class basic
{
public:
virtual void one()=0;
};
class derived1:public basic
{
public:
virtual void two()=0;
};
class derived2:pubilc base
{
public:
virtual void three()=0;
};
class component : public derived,public derived2
{
public:
void one(){ cout<
void two(){cout<
void three(){cout<
};
void main()
{
//
basic * b = (derived1*)new component;
cout<<*b;
b->one();
}
My question is why size of *b and size of *this are different.
sizeof(*b) will give the size of the base class (4)while
sizeof(*this)gives the size of component class( although the pointer
used here is *b i.e basic class pointer.
regards,
Bob
|
|
| Back to top |
|
 |
John Harrison Guest
|
Posted: Sat Oct 30, 2004 8:33 am Post subject: Re: why size of *this and size of basic and derived class di |
|
|
"Bob" <bob_peterson (AT) rediffmail (DOT) com> wrote
| Quote: | Try the following code
class basic
{
public:
virtual void one()=0;
};
class derived1:public basic
{
public:
virtual void two()=0;
};
class derived2:pubilc base
{
public:
virtual void three()=0;
};
class component : public derived,public derived2
{
public:
void one(){ cout<
void two(){cout<
void three(){cout<
};
void main()
{
//
basic * b = (derived1*)new component;
cout<<*b;
|
I guess you mean
cout<
| Quote: | b->one();
}
My question is why size of *b and size of *this are different.
sizeof(*b) will give the size of the base class (4)while
sizeof(*this)gives the size of component class( although the pointer
used here is *b i.e basic class pointer.
|
That's not right, the method one() is in the Component class so 'this' has
type Component*, therefore sizeof(*this) equals sizeof(Component).
Not sure why you think it would be any different, but remember that sizeof
is worked out at compile time. The number printed by the method one() will
always be the same no matter what pointer is used to call it.
john
|
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|