473,654 Members | 3,104 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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(*t his);}
void two(){cout<<siz eof(*this);}
void three(){cout<<s izeof(*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)gi ves 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 1236

"Bob" <bo**********@r ediffmail.com> wrote in message
news:e8******** *************** ***@posting.goo gle.com...
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(*t his);}
void two(){cout<<siz eof(*this);}
void three(){cout<<s izeof(*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)gi ves 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(Componen t).

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
4529
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 go to great lengths to restructure my code just to look concise and make it more manageable. When I say this, I'm also referring to the way I write my functions. It seems to me sometimes that I shouldn't have many void functions accepting...
4
4424
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 one vtbl ptr BUt when I derive the class B from class A (both have 1 virtual function each ) the size remains still as 4 bytes . class A = 4 bytes class B :public A = 4 bytes class...
3
1804
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 little off of what I was looking for. Hence this new thread. Thanks, ThazKool
7
2232
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 me. So, I'm here for an answer (more of a confirmation), hopefully. First let me say that I know people keep saying; it doesn't work because the member "is a private". I believe there's more to it than just simply that... Theory: You inherit,...
8
2562
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 size of an object in a "virtual" manner ? For instance, how can I write a virtual function print_size(); which prints the size of *this, and which will print the correct value with
45
2884
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 to check. What I expected was that there would be minor code bloat and some speed improvement when using templates. However... I wrote a basic list container (using templates), and a list container (using virtual derived classes). I also tried...
5
1432
by: subkriskat | last post by:
have a look at the sample code class myTest { private: int i; int d; }; class myTest2:myTest { private:
11
1468
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. Then in my base class I for example have a pure virtual function called eg PerformAction. Then in each message class I implement PerformAction. The base class works out what the type of message is and then calls the correct inherited class...
3
1222
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 regard to Derived class. In other words, Base class should be at least visible/ accessible as derived. 3. Can we member hiding by re-implementing the method in the derived
0
8379
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8816
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8709
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8494
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8596
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7309
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5627
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
2
1924
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1597
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.