C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

why size of *this and size of basic and derived class differ

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language (comp.lang.c++)
View previous topic :: View next topic  
Author Message
Bob
Guest





PostPosted: Sat Oct 30, 2004 7:48 am    Post subject: why size of *this and size of basic and derived class differ Reply with 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;
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(Cool although the pointer
used here is *b i.e basic class pointer.

regards,
Bob
Back to top
John Harrison
Guest





PostPosted: Sat Oct 30, 2004 8:33 am    Post subject: Re: why size of *this and size of basic and derived class di Reply with quote




"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(Cool 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
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language (comp.lang.c++) All times are GMT
Page 1 of 1

 
Jump to:  
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


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.