Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does a compiler add to an empty class declaration?

Suppose, I write

class A { };

The compiler should provide (as and when needed)

  1. a constructor
  2. a destructor
  3. a copy constructor
  4. = operator

Is this all the compiler provides? Are there any additions or deletions to this list?

like image 226
Moeb Avatar asked Apr 17 '10 20:04

Moeb


1 Answers

It's complete. But there are two points you should note:

  1. It's the copy =operator. Just like there is a copy constructor, there is a copy assignment operator.
  2. They are only provided if actually used.

Some explanation for 2:

struct A { private: A(); };
struct B : A { };

That's fine! Providing a default constructor would be ill-formed for "B", because it would not be able to call the base-class' constructor. But the default constructor (and the other special functions) is only provided (we say it's implicitly defined) if it's actually needed.

like image 62
Johannes Schaub - litb Avatar answered Oct 02 '22 14:10

Johannes Schaub - litb