gpt4 book ai didi

c++ - 具有已删除方法的 C++ 类是否可以轻松复制?

转载 作者:塔克拉玛干 更新时间:2023-11-03 01:06:56 25 4
gpt4 key购买 nike

我希望类 B 继承类 A 的所有方法,但只有少数方法(假设它是可平凡复制的),并且仍然是平凡可复制的。在 C++11 中,我可以删除方法。举个例子:

class A { // trivially copyable
// private stuff here
public:
A& operator += (const A&);
// other public stuff here
};

class B: public A {
public:
B& operator += (const A&) = delete;
};

B 是否可以平凡复制?我知道删除特殊方法存在问题,但复合赋值不是特殊方法(对吧?)。

最佳答案

是的,B 是可以简单复制的——不管你对非特殊成员函数做了什么。

N3337, §9/6:

A trivially copyable class is a class that:
— has no non-trivial copy constructors (12.8),
— has no non-trivial move constructors (12.8),
— has no non-trivial copy assignment operators (13.5.3, 12.8),
— has no non-trivial move assignment operators (13.5.3, 12.8), and
— has a trivial destructor (12.4).

but the compound assignment is not a special method (right?)

不,不是。

N3337, §12/1:

The default constructor (12.1), copy constructor and copy assignment operator (12.8), move constructor and move assignment operator (12.8), and destructor (12.4) are special member functions.

关于c++ - 具有已删除方法的 C++ 类是否可以轻松复制?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25830995/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com