Empty Classes

Empty Classes - class A {}; void f() { A* p1 = new A; A* p2...

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
Empty Classes Here's a simple one. In C, an empty struct like: struct A {}; is invalid, whereas in C++ usage like: struct A {}; or: class B {}; is perfectly legal. This type of construct is useful when developing a skeleton or placeholder for a class. An empty class has size greater than zero. Two class objects of empty classes will have distinct addresses, as in:
Background image of page 1
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: class A {}; void f() { A* p1 = new A; A* p2 = new A; // p1 != p2 at this point . .. } There are still one or two C++ compilers that generate C code as their "assembly" language. To handle an empty class, they will generate a dummy member, so for example: class A {}; becomes: struct A { char __dummy; }; in the C output. \...
View Full Document

Ask a homework question - tutors are online