抽象类、复制构造函数

发布于 2024-10-31 13:31:48 字数 45 浏览 0 评论 0原文

在具有纯虚方法的类中或仅在派生类中定义复制构造函数/运算符 = 是否有意义?

Does it make sense to define a copy constructor / operator = in class having pure virtual method or only in derived classes?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

随波逐流 2024-11-07 13:31:48

如果它只有纯虚拟方法(并且没有数据成员),那么,不,合成的方法很好(并且不会做任何事情)。

如果它确实有数据成员,那么您应该定义自己的复制构造函数(如果/当这样做有意义时),就像任何其他类一样。派生类实际上与此没有太大关系。

If it has only pure virtual methods (and no data members) then, no, the synthesised one is fine (and won't do anything).

If it does have data members then you should define your own copy constructor if/when it makes sense to do so, just like for any other class. Derived classes don't really have much to do with it.

闻呓 2024-11-07 13:31:48

像普通类一样:是的,如果您有特定的实现需求。

like normal classes: yes, if you have a specific implementation need.

数理化全能战士 2024-11-07 13:31:48

如果它是您计划复制的对象,是的,这是一个好主意。如果不是,请参阅下面我的评论。

如果您的虚拟基类依赖于需要显式分配和复制的资源(缓冲区、操作系统对象等),则定义复制构造函数可以节省您在每个派生类中单独执行此操作的麻烦(此外,如果这些基础资源是私有的,则无法使用公共继承)。

例如:

class Base {
public:
    Base( const Base & );
    virtual ~Base();
    virtual void Method() = 0;
    // etc...
private:
    int *memberIntArray;
    int length;
    // etc...
};

class DerivedOne : public Base{
public:
    DerivedOne( const DerivedOne & );
    virtual ~DerivedOne();
    virtual void Method();
    // etc...
protected:
    // data, etc...
};

class DerivedTwo : public Base{
public:
    DerivedTwo( const DerivedTwo & );
    virtual ~DerivedTwo();
    virtual void Method();
    // etc...
protected:
    // data, etc...
};

///////////////////

Base::Base( const Base & other ) {
    this->length = other.length;
    this->memberIntArray = new int[length];
    memcpy(this->memberIntArray,other.memberIntArray,length);
}

//etc...

DerivedOne::DerivedOne( const DerivedOne & other )
    : Base( other )
{
    //etc...
}

DerivedTwo::DerivedTwo( const DerivedTwo & other )
    : Base( other )
{
    //etc...
}

If it is an object you plan on copying, yes, it's a good idea. See my comment below for when it isn't.

If your virtual base class relies on resources that need to be explicitly allocated and copied (buffers, operating system objects, etc.), defining a copy constructor saves you the trouble of doing so in every derived class separately (and, additionally, is something you couldn't do if those base resources were private, using public inheritance).

E.g.:

class Base {
public:
    Base( const Base & );
    virtual ~Base();
    virtual void Method() = 0;
    // etc...
private:
    int *memberIntArray;
    int length;
    // etc...
};

class DerivedOne : public Base{
public:
    DerivedOne( const DerivedOne & );
    virtual ~DerivedOne();
    virtual void Method();
    // etc...
protected:
    // data, etc...
};

class DerivedTwo : public Base{
public:
    DerivedTwo( const DerivedTwo & );
    virtual ~DerivedTwo();
    virtual void Method();
    // etc...
protected:
    // data, etc...
};

///////////////////

Base::Base( const Base & other ) {
    this->length = other.length;
    this->memberIntArray = new int[length];
    memcpy(this->memberIntArray,other.memberIntArray,length);
}

//etc...

DerivedOne::DerivedOne( const DerivedOne & other )
    : Base( other )
{
    //etc...
}

DerivedTwo::DerivedTwo( const DerivedTwo & other )
    : Base( other )
{
    //etc...
}
被翻牌 2024-11-07 13:31:48

是的,你应该这样做。
拥有自己的类复制构造函数、复制赋值运算符和析构函数实现的规则甚至适用于抽象类。
另外,请查看三法则

Yes you should.
Rules of having your own implementations for copy constructor, copy assignment operator and destructor for a Class will apply to even an Abstract Class.
Also, have a look at Rule of Three

许一世地老天荒 2024-11-07 13:31:48

这取决于您的使用情况,如果您没有做任何需要精细处理复制的事情,例如复制句柄。如果需要,最好在派生类中定义复制构造函数。

It depends on your usage, if you are not doing anything that requires copying to handled delicately, e.g. copying a handle. You would be better to define a copy constructor in derived classes if required.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文