Reputation: 763
I am confused about class size of these two classes.
who can tell me about why the output of this is " SIZEOF(X)=4; SIZEOF(Y)=8;
or provide some useful links/resources about this question?
anyone can help me?
#include <iostream>
using namespace std;
class X {
int i;
public:
X() { i = 0; }
void set(int ii) { i = ii; }
int read() const { return i; }
int permute() { return i = i * 47; }
};
class Y : public X {
int i; // Different from X's i
public:
Y() { i = 0; }
int change() {
i = permute(); // Different name call
return i;
}
void set(int ii) {
i = ii;
X::set(ii); // Same-name function call
}
};
int main() {
cout << "sizeof(X) = " << sizeof(X) << endl;
cout << "sizeof(Y) = "
<< sizeof(Y) << endl;
Y D;
D.change();
// X function interface comes through:
D.read();
D.permute();
// Redefined functions hide base versions:
D.set(12);
getchar();
} ///:~
Upvotes: 1
Views: 1375
Reputation: 513
Here there are some related website, I think it can be helpful to you.
Determine the size of class object: http://www.cprogramming.com/tutorial/size_of_class_object.html
Memory layout: http://www.phpcompiler.org/articles/virtualinheritance.html
And, if you use MVSC, you can dump all memory layout of all class in your solution with -d1reportAllClassLayout like that:
cl -d1reportAllClassLayout main.cpp
Upvotes: 0
Reputation: 45948
I think the question title suggests that you think these classes have a larger size, due to their member functions and base classes. Well, this is wrong.
In final compiled code, the member functions aren't any different from free functions, just with an implicit first parameter of this
, so they don't take any per-object space and thus don't count to the size of the type.
Furthermore, mere base classes don't impose any additional memory usage, they (their members) are just stored alongside the derived class's members. Although you are hiding X
's i
in Y
, it is still there (and can be accessed by explicit specification (X::i
)) and takes space in addition to Y
's i
, thus giving two int
s of 4 bytes each.
The situation would be slightly different if you had any virtual member functions. In this case there would be a small memory overhead for the additional indirection (most probably in a pointer to a list of virtual function addresses, being 4/8 bytes).
Upvotes: 1
Reputation: 104698
sizeof(X)
returns the size of an instance of X
. On your system, the sizeof an int is 4. Then you extend the class with Y and its data. This class is not polymorphic, and does not require additional space (e.g. for vtables) or padding (to adjust for natural alignment).
Upvotes: 0
Reputation: 17114
The code inside the class only exists in one place (the same as a static
data member). It isn't duplicated for each class instance. So it doesn't count towards the size of the class.
Upvotes: 0
Reputation: 253
Because X contains one integer and Y contains 2 (one of them is inherited). The size of an integer on you machine is 4 bytes.
Upvotes: 0
Reputation: 13099
X contains one int of four bytes.
Y contains two int's of four bytes each, as it also holds X's member variable, ie eight bytes total.
Upvotes: 3