Questions tagged [empty-class]

21 questions
61
votes
4 answers

Empty class object in Python

I'm teaching a Python class on object-oriented programming and as I'm brushing up on how to explain classes, I saw an empty class definition: class Employee: pass The example then goes on to define a name and other attributes for an object of…
Ramy
  • 20,541
  • 41
  • 103
  • 153
44
votes
3 answers

What function does C++ write and call in an empty class?

In the book Effective C++, I saw the passage below: As a result, if you write class Empty{}; it's essentially the same as if you'd written this: class Empty { public: Empty() { ... } Empty(const Empty& rhs) { ... } ~Empty() { ...…
llinvokerl
  • 1,029
  • 10
  • 25
16
votes
3 answers

Why is the empty base class optimization (EBO) is not working in MSVC?

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 struct T1 { }; struct T2 { }; struct T3…
user541686
  • 205,094
  • 128
  • 528
  • 886
15
votes
8 answers

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

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++.
garima
  • 5,154
  • 11
  • 46
  • 77
11
votes
2 answers

why a const object of an empty class cant be created

#include class A { public: void foo() const { std::cout << "const version of foo" << std::endl; } void foo() { std::cout << "none const version of foo" << std::endl; } }; int main() { A…
Haiyuan Zhang
  • 40,802
  • 41
  • 107
  • 134
11
votes
3 answers

Handles Comparison: empty classes vs. undefined classes vs. void*

Microsoft's GDI+ defines many empty classes to be treated as handles internally. For example, (source GdiPlusGpStubs.h) //Approach 1 class GpGraphics {}; class GpBrush {}; class GpTexture : public GpBrush {}; class GpSolidFill : public GpBrush…
Nawaz
  • 353,942
  • 115
  • 666
  • 851
9
votes
1 answer

Something about a completely empty class

#include using namespace std; class Empty{ char omg[0]; }; int main() { Empty em1, em2; Empty set[100]; cout << sizeof(Empty) << " " << sizeof(em1) << " " << sizeof(em2) << endl; cout << (long*)&em1 << " " <<…
simon_xia
  • 2,394
  • 1
  • 20
  • 32
9
votes
4 answers

Generating empty objects - empty class with empty properties/subclasses C#

I have two classes: public class HumanProperties { int prop1; int prop2; string name;} public class Human{int age; HumanProperties properties;} Now if i want to create new instance of Human, i have to do Human person = new Human(); But when i try…
Muno
  • 749
  • 2
  • 9
  • 19
4
votes
2 answers

Why sizeof(Derived4) is 8 byte? I think it should be 5 bytes

This is the output of the given program: sizeof(Empty) 1 sizeof(Derived1) 1 sizeof(Derived2) 4 sizeof(Derived3) 1 sizeof(Derived4) 8 sizeof(Dummy) 1 This is the program: #include using namespace std; class Empty {}; class Derived1 :…
Devesh Agrawal
  • 8,982
  • 16
  • 82
  • 131
3
votes
1 answer

Is there something like is_empty_but_has_virtual_functions?

I would like to craft an "interface"/mix-in class (template), and ensure nobody thinks that adding a member to this class template is a good idea, I'd like to static_assert on this condition. Unfortunately, std::is_empty does not allow virtual…
rubenvb
  • 74,642
  • 33
  • 187
  • 332
3
votes
2 answers

Construct an empty object without the default constructor

Suppose I have a type F. I know that F is empty, but F has no default constructor, so I can't use F() to construct it. Is there a way to obtain a valid object of type F anyway? I seem to recall a mention that there was such a way with arcane usage…
Justin
  • 24,288
  • 12
  • 92
  • 142
3
votes
1 answer

Chained inheritance of empty classes, still necessary?

Are there still good reasons to use chainned base cases rather than a flat multiple inheritance? It used to be the case that some classes were designed to be inherted in chain. I think this was to force the empty base class optimization. I…
alfC
  • 14,261
  • 4
  • 67
  • 118
2
votes
1 answer

Do we ever need empty class in java project? If yes then why?

In empty class (without field and properties) also compiler automatically creates a default constructor after compiling. If we never use this empty class then why compiler creates this default constructor?
Raghunath
  • 29
  • 4
2
votes
1 answer

C++ Exceptions Throw Empty Class

Below is an excerpt from "Programming: Principles and Practice Using C++". I'm confused by the throw Bad_area() notation. The book tries to explain it, "Bad_area() means 'Make an object of type Bad_area'," continuing with that it then throws that…
Jay Adams
  • 23
  • 1
  • 3
1
vote
1 answer

Why c++ empty class have no byte alignment?

I recently learned that empty class have size 1 instead of zero.Why it has no byte alignment, in which it's size should be 4 in 32bit environment? What's the address of the next object?
uuuvv
  • 15
  • 2
1
2