编译器生成的构造函数 [英] compiler generated constructors

查看:38
本文介绍了编译器生成的构造函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这只是一个快速的问题,可以正确理解当您使用这样的构造函数创建类时会发生什么:

This is just a quick question to understand correctly what happens when you create a class with a constructor like this:

class A
{
  public:
    A() {}
};

我知道没有生成默认构造函数,因为它已经定义了但是是编译器生成的复制和赋值构造函数,或者换句话说我需要声明一个私有拷贝构造函数和一个私有赋值操作符是为了防止这种情况发生?

I know that no default constructor is generated since it is already defined but are copy and assignment constructors generated by the compiler or in other words do i need to declare a private copy constructor and a private assignment operator in order to prevent this from happening?

class A
{
  private:
    // needed to prevent automatic generation?
    A( const A& );
    A& operator=( const A& );
  public:
    A() {}
};

推荐答案

是的.复制构造函数、赋值运算符和析构函数总是被创建而不管其他构造函数和运算符.

Yes. The copy constructor, assignment operator, and destructor are always created regardless of other constructors and operators.

如果你想禁用一个,你有什么是完美的.这也很常见.

If you want to disable one, what you've got there is perfect. It's quite common too.

这篇关于编译器生成的构造函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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