从重载的拷贝构造函数中调用默认拷贝构造函数 [英] Call default copy constructor from within overloaded copy constructor

查看:324
本文介绍了从重载的拷贝构造函数中调用默认拷贝构造函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要编写一个复制构造函数,深度复制 std :: shared_ptr 的内容。但是,还有一堆变量 int a,b,c,d,e; 也在类中定义。是否有一种方法来生成我的新的重载的默认复制构造函数代码(或调用默认的复制构造函数)。

I need to write a copy constructor that deep copies the contents of a std::shared_ptr. However, there are a bunch of variable int a, b, c, d, e; also defined in the class. Is there a way to generate the default copy constructor code (or call the default copy constructor) inside my new overloaded one.

这里是一个代码片段,澄清问题。

Here is a code snippet with a comment that hopefully clarifies the issue.

class Foo {
public:
     Foo() {}
     Foo(Foo const & other);
     ...
private:
     int a, b, c, d, e;
     std::shared_ptr<Bla> p;
};

Foo::Foo(Foo const & other) {
    p.reset(new Bla(*other.p));

    // Can I avoid having to write the default copy constructor code below
    a = other.a;
    b = other.b;
    c = other.c;
    d = other.d;
    e = other.e;
}


推荐答案

这里的问题代码作为我写这个:

Here’s the question code as I’m writing this:

class Foo {
public:
     Foo() {}
     Foo(Foo const & other);
     ...
private:
     int a, b, c, d, e;
     std::shared_ptr<Bla> p;
};

Foo::Foo(Foo const & other) {
    p.reset(new Bla(other.p));

    // Can I avoid having to write the default copy constructor code below
    a = other.a;
    b = other.b;
    c = other.c;
    d = other.d;
    e = other.e;
}

上述代码很可能错误,因为


  1. 默认构造函数留下 a b c d e 未初始化,

代码不负责作业复制,

表达式 new Bla(other.p)要求 Bla 有一个构造函数取<$ c $

the expression new Bla(other.p) requires that Bla has a constructor taking a std::shared_ptr<Bla>, which is extremely unlikely.

std :: shared_ptr 这将是C ++ 11代码,以正式语言正确。但是,我相信它只是使用编译器提供的&rsquos的代码。因此我相信相关的C ++标准是C ++ 98,并且对C ++ 03修订版进行了技术更正。

With std::shared_ptr this would have to be C++11 code in order to be formally correct language-wise. However, I believe that it’s just code that uses what’s available with your compiler. And so I believe that the relevant C++ standard is C++98, with the technical corrections of the C++03 amendment.

您可以轻松地利用内置(生成)复制初始化,即使在C ++ 98中,例如

You can easily leverage the built-in (generated) copy initialization, even in C++98, e.g.

namespace detail {
    struct AutoClonedBla {
        std::shared_ptr<Bla> p;

        AutoClonedBla( Bla* pNew ): p( pNew ) {}

        AutoClonedBla( AutoClonedBla const& other )
            : p( new Bla( *other.p ) )
        {}

        void swap( AutoClonedBla& other )
        {
            using std::swap;
            swap( p, other.p );
        }

        AutoClonedBla& operator=( AutoClonedBla other )
        {
            other.swap( *this );
            return *this;
        }
    };
}

class Foo {
public:
     Foo(): a(), b(), c(), d(), e(), autoP( new Bla ) {}
     // Copy constructor generated by compiler, OK.

private:
     int                      a, b, c, d, e;
     detail::AutoClonedBla    autoP;
};



< swap成语),并且不需要特殊的智能 - 指向感知的 Bla 构造函数,而只是使用普通的 Bla 复制构造函数来复制。

Note that this code does initialize correctly in the default constructor, does take charge of copy assignment (employing the swap idiom for that), and does not require a special smart-pointer-aware Bla constructor, but instead just uses the ordinary Bla copy constructor to copy.

这篇关于从重载的拷贝构造函数中调用默认拷贝构造函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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