empty-class

what is the size of empty class in C++,java?

落花浮王杯 提交于 2019-11-27 03:53:34
问题 What is the size of an empty class in C++ and Java? Why is it not zero? sizeof(); returns 1 in the case of C++. 回答1: Short Answer: The standard explicitly says that a class can not have zero size. Long Answer: Because each object needs to have a unique address (also defined in the standard) you can't really have zero sized objects. Imagine an array of zero sized objects. Because they have zero size they would all line up on the same address location. So it is easier to say that objects can

Why empty base class optimization is not working?

给你一囗甜甜゛ 提交于 2019-11-26 14:41:58
问题 Why is the empty base class optimization (EBO) not being fully applied in Visual C++? If I have a lot of base classes, is there any way for me to help the compiler make this optimization? #include <iostream> struct T1 { }; struct T2 { }; struct T3 { }; struct T4 { }; struct T5 { }; struct T6 { }; struct Test : T1, T2, T3, T4, T5, T6 { }; int main() { std::cout << sizeof(Test); } // Prints 5 回答1: This is a longstanding bug in the Visual C++ compiler. When a class derives from multiple empty