当在C ++中生成默认构造函数时 [英] When is default constructor generated in C++

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

问题描述

根据内部C ++对象模型,只有当以下四个条件中的至少任何一个为真时,编译器才会生成一个复制构造函数(如果未由程序员声明):


  1. 当类包含一个复制构造函数存在的类的成员对象时(由类设计器显式声明,如前一个String类的情况


  2. 当类是从存在复制构造函数的基类派生时(再次


  3. 当类声明一个或多个虚函数时

    p>当类是从一个或多个基类是虚拟的继承链派生时


我有一个类只有构造函数,然后复制构造函数将不会由编译器提供。



让我们举个例子:

  class test 
{
test(){}
};
int main()
{
test obj1; // statement 1
test obj2(obj1); // statement 2
}

现在问题出现时,我添加以下行在类test中:

  test(const test& rhs)= delete; 

= delete确保不会自动提供复制构造函数。添加上面的行后,我得到语句2的一个错误,说:使用删除的函数test :: test(const test&)



我的问题是:根据内部C ++对象模型我不需要一个复制构造函数为上述类,所以当我明确地说不生成一个复制构造函数(使用删除)为什么我一个错误?因为我期望编译器不需要为上面的类复制构造函数。



我使用gcc版本4.6.3。

解决方案

对于要复制的类,它需要有一个复制构造函数。无论你自己编写,还是编译器为你生成一个,都没关系 - 它必须可用于 test a;



您明确强制编译器删除复制构造函数 - 这是旧版本的旧版本make copy constructor privatetrick。它禁止复制。所以不要惊讶,你不能复制。因为你告诉编译器不允许它。


As per "Inside C++ Object Model" a copy constructor is generated (if not declared by the programmer) by the compiler only when at least any one of the below four conditions are true:

  1. When the class contains a member object of a class for which a copy constructor exists (either explicitly declared by the class designer, as in the case of the previous String class, or synthesized by the compiler, as in the case of class Word)

  2. When the class is derived from a base class for which a copy constructor exists (again, either explicitly declared or synthesized)

  3. When the class declares one or more virtual functions

  4. When the class is derived from an inheritance chain in which one or more base classes are virtual

Which means if I have a class with just constructor then copy constructor will not be provided by the compiler.

Lets take an example:

class test
{
    test(){}
};
int main()
{
    test obj1;       //statement 1
    test obj2(obj1); //statement 2
}

Above code works fine. Now the problem comes when I add the following lines in class test:

test(const test& rhs) = delete;

"= delete" ensures that copy constructor is not automatically provided. After adding above line I am getting an error for statement 2 which says Use of deleted function test::test(const test&).

My question is: as per "Inside C++ Object Model" I don't need a copy constructor for the above class so when I am explicitly saying not to generate a copy constructor (using delete) why am I getting an error? Since I was expecting that the compiler won't need a copy constructor for the above class.

I am using gcc version 4.6.3.

解决方案

For class to be copyable, it needs to have a copy constructor. Whether you write your own, or compiler generates one for you, it doesn't matter — it has to be available for test a; test b(a); to be a valid operation.

You explicitly force compiler to delete the copy constructor — this is new version of old "make copy constructor private" trick. It disallows copying. So don't be surprised that you can't copy. Because you told the compiler to not allow it.

这篇关于当在C ++中生成默认构造函数时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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