I have the following C++ code:

class A {
    private: 
           long num1;
};

class B : public A {
    private: 
           void f1();
};

class C: public B {
    private:
           virtual void f2();
};

int main()
{
   cout <<sizeof(A)<<endl;
   cout <<sizeof(B)<<endl;
   cout<<sizeof(C)<<end;
}

Would someone please help me with the output and please explain? Thanks!

Recommended Answers

All 19 Replies

Well i believe that you cannot use Sizeof() function on class.

DO create objects and then find the size of objects to find sixe of class

Like

class A a;


cout<<sizeof(a);

Please correct me if i am wrong at some point ....i am not in touch with C,C++ or long time

Please use code tags while posting code.
What do you not understand in the output? If you are wondering about increase in size of class C then read about virtual functions, virtual table, virtual pointer etc.

Well i believe that you cannot use Sizeof() function on class.

sizeof() is not a function but compile time operator in C++. You can very well apply it on user defined types.

Thanks for the reply.

I actually don't understand the size of B. There's only one private function in B. What made the size to be 4? For A, because the long int takes 4 bytes, I can understand the size is 4.

Thanks!

Please use code tags while posting code.
What do you not understand in the output? If you are wondering about increase in size of class C then read about virtual functions, virtual table, virtual pointer etc.

>There's only one private function in B. What made the size to be 4?
Member functions don't generally apply toward the size of an object. If they do, I would question the quality of your compiler. The thing that made the size be 4 is the internal object stuff that C++ does behind your back, in this case, probably the pointer for a virtual table..

commented: Nice ~~SunnyPalSingh +3

Thanks for the reply.

I actually don't understand the size of B. There's only one private function in B. What made the size to be 4? For A, because the long int takes 4 bytes, I can understand the size is 4.

Thanks!

class B also includes class A, therefore sizeof(B) is 4 because class A contains a long integer, which on most compilers sizeof(long) = 4. If you remove class A from class B, then sizeof(B) = 1, which is the smallest amount allowed for any class.

>There's only one private function in B. What made the size to be 4?
Member functions don't generally apply toward the size of an object. If they do, I would question the quality of your compiler. The thing that made the size be 4 is the internal object stuff that C++ does behind your back, in this case, probably the pointer for a virtual table..

But class B doesn't have any virtual function? So what's the point of pointer to virtual table for class B?
Isn't the sizeof class B 4 because of the subobject A?

But the problem is that the 'long integer' in class A is private, so, it's invisible to B, and B can not access it. How can it contribute to B's size?

class B also includes class A, therefore sizeof(B) is 4 because class A contains a long integer, which on most compilers sizeof(long) = 4. If you remove class A from class B, then sizeof(B) = 1, which is the smallest amount allowed for any class.

But class B doesn't have any virtual function? So what's the point of pointer to virtual table for class B?
Isn't the sizeof class B 4 because of the subobject A?

Narue was speaking in general terms. VTables do influence class size, just as do data members of superclasses.

Narue was speaking in general terms. VTables do influence class size, just as do data members of superclasses.

Actually she was quoting something else so I was thinking that. I don't doubt about her knowledge.

But the problem is that the 'long integer' in class A is private, so, it's invisible to B, and B can not access it. How can it contribute to B's size?

Yes, it is not accessible but derived class contains a subobject of the type of the base class.

I think I'm clear now. Thanks for all the replies and for the code tags. I'll learn to use it next time. :)

>Actually she was quoting something else so I was thinking that.
Actually, I didn't look at the code closely enough, so my conclusion didn't match the quote. This is one of the rare cases where I was completely wrong. ;) I think in the future I'll refrain from answering programming questions while waiting to zone in EQ2. :rolleyes:

Narue wrong on a C++ question?? This thread is getting stickied to commemorate!

Narue wrong on a C++ question?? This thread is getting stickied to commemorate!

Narue WRONG ????

NAH, can't be, it was just a fault in the Matrix :cheesy:

The only problem with being right all of the time is that people really let you have it when you make a mistake. ;)

Your answer was correct, just not for that particular question.
So it wasn't your mistake at all, the OP should have asked a different question to match your answer!

>the OP should have asked a different question to match your answer!
Yea! How dare he not tailor his question to my answer! Jeez, these people sure are arrogant and presumptuous. ;)

The only problem with being right all of the time is that people really let you have it when you make a mistake.

and the good thing about being err...rough is that they all support you when you don't get it right for the fear you might fly in fury mode...;)

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.