一个类有多少默认方法? [英] How Many default methods does a class have?

查看:192
本文介绍了一个类有多少默认方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对不起,这可能看起来很简单,但有人问我这个,我不知道确定。



一个空的C ++类有什么功能? / p>

构造函数,
复制构造函数,
赋值,
析构函数



是吗?

解决方案

在C ++ 03中有4个:




  • 默认构造函数:只有在
    没有用户定义的构造函数声明为
    时才声明。使用时定义


  • 复制构造函数 - 仅在用户尚未声明时声明。


  • 破坏者与上述相同







  • 移动构造函数




编译器也可能无法生成其中的一些。例如,如果类包含例如引用(或任何不能被复制分配的其他内容),那么编译器将无法为您生成副本赋值运算符。 有关详情,请阅读此


Sorry, this might seem simple, but somebody asked me this, and I don't know for certain.

An empty C++ class comes with what functions?

Constructor, Copy Constructor, Assignment, Destructor?

Is that it? Or are there more?

解决方案

In C++03 there are 4:

  • Default constructor: Declared only if no user-defined constructor is declared. Defined when used

  • Copy constructor - declared only if the user hasn't declared one. Defined if used

  • Copy-assignment operator same as above

  • Destructor same as above

In C++11 there are two more:

  • Move constructor
  • Move-assignment operator

It is also possible that the compiler won't be able to generate some of them. For example, if the class contains, for example, a reference (or anything else that cannot be copy-assigned), then the compiler won't be able to generate a copy-assignment operator for you. For more information read this

这篇关于一个类有多少默认方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆