What does a compiler add to an empty class declaration?
Suppose, I write
class A { };
The compiler should provide (as and when needed)
- a constructor
- a destructor
- a copy constructor
- = operator
Is this all the compiler provides? A开发者_如何学Pythonre there any additions or deletions to this list?
It's complete. But there are two points you should note:
- It's the copy =operator. Just like there is a copy constructor, there is a copy assignment operator.
- 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.
From C++11 onwards, in addition to what you have listed
- Move ctor
- Move assignment operator
Your list is complete. This is all it is adding.
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.
There are five properties:
constructor
copy constructor
destructor
assignment operator
the reference operator(&) - the address
精彩评论