Hi,
pls try to observe the size(i.e. total) of object for below program and in different cases as commented out.

#include<iostream>
using namespace std;
class B
{
int x; // try by commenting either data member and observe the object size 
int *p;
public:
virtual void show(){}
};

int main()
{
B o;
cout << sizeof(o)<< endl;
return 0;
}

Individually x contibute 4 byte and p contribute 8 byte but both together make object size to 16 byte, why it is.

Thanks
dipesh udhlani

This link explains better

Hi,
I found below link is good to understand
http://www.viva64.com/en/b/0004/

The made conclusion is that size of object is depends on, 1. order of data member(DM) declaration in class and 2. object size is always in multiple of size of it's maxm. size DM.

Thanks and Regards
dipesh udhlani

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.