473,385 Members | 1,732 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

why size of *this and size of basic and derived class different?

Bob
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<<sizeof(*this);}
void two(){cout<<sizeof(*this);}
void three(){cout<<sizeof(*this);}


};

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(8) although the pointer
used here is *b i.e basic class pointer.

regards,
Bob
Jul 22 '05 #1
1 1230

"Bob" <bo**********@rediffmail.com> wrote in message
news:e8**************************@posting.google.c om...
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<<sizeof(*this);}
void two(){cout<<sizeof(*this);}
void three(){cout<<sizeof(*this);}


};

void main()
{
//
basic * b = (derived1*)new component;
cout<<*b;
I guess you mean

cout<<sizeof(*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(8) 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
Jul 22 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

35
by: wired | last post by:
Hi, I've just taught myself C++, so I haven't learnt much about style or the like from any single source, and I'm quite styleless as a result. But at the same time, I really want nice code and I...
4
by: vijay | last post by:
I have a doubt with size of classed with virtual functions I have declared A,A1,A2 ,B , C, D some classes with no varaibles but a vitual function each, The size of A is as expected 4 bytes with...
3
by: ThazKool | last post by:
Is there anyway to write a class or struct that has no storage. It only operates on a reference to an already existing type. This is actually an extension to another thread. The thread went a...
7
by: relient | last post by:
Question: Why can't you access a private inherited field from a base class in a derived class? I have a *theory* of how this works, of which, I'm not completely sure of but makes logical sense to...
8
by: François Fleuret | last post by:
Dear all, I fear this is a question which has been asked zillions of times. However, it seems to me to be such a legitimate feature that I can't imagine it is impossible. How can one know the...
45
by: charles.lobo | last post by:
Hi, I have recently begun using templates in C++ and have found it to be quite useful. However, hearing stories of code bloat and assorted problems I decided to write a couple of small programs...
5
by: subkriskat | last post by:
have a look at the sample code class myTest { private: int i; int d; }; class myTest2:myTest { private:
11
by: Angus | last post by:
I am developing a server which receives a range of different messages. There are about 12 different message types so I thought that a good idea would be to devise a class for each message type....
3
by: puzzlecracker | last post by:
.... I am coming off c++, so here is the list of question, I'd like to get clarifications. 1. What's and the point of static constructor? 2. Why C# differs as to base class visibility in...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.