2

A C++ class has 4 essential functions: Constructor, destructor, copy constructor and assignment operator. You are supposed to define them explicitly, but if you don't, the compiler will generate them for you. But as Scott Meyer says, compiler will only generate them if it needs to. There are many conditions, but for example, if a class is bit-wise copyable, compiler will not generate a copy constructor.

So if I have an empty class as this:

class A {};

I would assume, compiler is not going to generate any functions for this. I have couple of questions:

  1. How do I see the compiler generated functions? For example, I am able to see my functions in the class symbol table calling nm on the object file. In the symbol table, although the names are mangled, I can clearly identify the functions I declared. But I cannot see any constructors, etc. How do I see the functions that are generated by the compiler?

  2. What does it mean to create an instance of an empty class? I know C++ puts a memory allocation of 1 to empty classes to make each instance have a unique address. But if there is no default compiler generated constructor, what does it mean to do:

    A a1 = A(); A *a1ptr = new A();

Or should it be

A a2 = A;

Cheers

madu
  • 5,232
  • 14
  • 56
  • 96
  • 1
    "You are supposed to define them explicitly" Usually you don't have to do this. – juanchopanza Oct 14 '14 at 15:30
  • 1
    [_C++11 compiler generated functions_](http://stackoverflow.com/q/19606492/1870232) – P0W Oct 14 '14 at 15:31
  • 1
    Maybe a corollary question should be asked: "When do I need to create the otherwise compiler generated functions?" – Thomas Matthews Oct 14 '14 at 15:45
  • Thank you guys. But I'm still not clear as what it means to instantiate an empty class without any compiler generated functions? Also, if there are compiler generated ones, how I can see which ones are generated, like in a symbol table. – madu Oct 14 '14 at 16:44
  • 1
    @madu - I gave you +1 a while ago, and I keep checking back. ' hoping someone is able to provide a good answer that doesn't resort to a ton of disassembly and reverse-engineering. We know what is suppose to get generated, but it would be nice to easily verify. – Jeff Oct 31 '14 at 19:25

0 Answers0