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?

1
  • 3
    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. Commented Feb 25, 2013 at 13:30

5 Answers 5

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.

Sign up to request clarification or add additional context in comments.

1 Comment

I guess you could argue that in the case of an empty class, the compiler also adds at least one byte of padding ;-)
3

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

  • Move ctor
  • Move assignment operator

Comments

1

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

Comments

-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.

Comments

-1

There are five properties:

constructor

copy constructor

destructor

assignment operator

the reference operator(&) - the address

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.