I know it might sound like a weird question, but I was just wondering if class in C++ weights more than a struct with the same data fields, and there's this one thing I couldn't find an answer for...
Consider this:
struct SomeStruct {
int a;
int b;
};
class SomeClass {
public:
SomeClass():a(0),b(0){}
private:
int a;
int b;
};
int main() {
std::cout<<sizeof(SomeStruct)<<std::endl; // output is 8
std::cout<<sizeof(SomeClass)<<std::endl; // output is 8
}
But now see what happens when I add a destructor to SomeClass:
struct SomeStruct {
int a;
int b;
};
class SomeClass {
public:
SomeClass():a(0),b(0){}
virtual ~SomeClass(){}
private:
int a;
int b;
};
int main() {
std::cout<<sizeof(SomeStruct)<<std::endl; // output is 8 bytes
std::cout<<sizeof(SomeClass)<<std::endl; // output is 16 bytes!
}
Why does SomeClass need 8 more bytes for the destructor?