What does a compiler add to an empty class declaration?

半世苍凉 提交于 2019-11-30 17:33:57

问题


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:


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.




回答2:


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




回答3:


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.




回答4:


There are five properties:

constructor

copy constructor

destructor

assignment operator

the reference operator(&) - the address



来源:https://stackoverflow.com/questions/2659895/what-does-a-compiler-add-to-an-empty-class-declaration

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!