What does a compiler add to an empty class declaration?
Asked Answered
H

5

6

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?

Hive answered 17/4, 2010 at 20:18 Comment(1)
I came across this old question just now... for the benefit of future visitors it's probably worth mentioning that in C++11 a move constructor as well as a move assignment operator are auto-generated in addition to the above.Chlorine
E
6

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.

Electromagnetic answered 17/4, 2010 at 20:40 Comment(1)
I guess you could argue that in the case of an empty class, the compiler also adds at least one byte of padding ;-)Leucomaine
H
3

From C++11 onwards, in addition to what you have listed

  • Move ctor
  • Move assignment operator
Haleigh answered 28/7, 2021 at 6:22 Comment(0)
J
1

Your list is complete. This is all it is adding.

Joke answered 17/4, 2010 at 20:25 Comment(0)
A
-1

The list is not completed............ In addition with the above mention FOUR properties , there is an address operator ( & ) overloaded method , that return the address of the invoking object , is also provided automatically by the compiler.

Arce answered 4/10, 2014 at 19:16 Comment(0)
F
-1

There are five properties:

constructor

copy constructor

destructor

assignment operator

the reference operator(&) - the address

Flag answered 11/11, 2014 at 13:41 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.