- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在实现一个简单的层次结构:System 和 Device 将是两个基类。系统有设备。然后我会有一个从系统派生的网络,它将有计算机。计算机派生自系统和设备。最后,计算机有各种设备,如 CPU、内存等。计算机是设备的原因是因为网络是系统,而系统存储设备。计算机之所以是系统,是因为它存储设备(CPU、内存等)
无论如何,到目前为止我的实现:
class Device {
public:
explicit Device(unsigned property): property(property) {}
friend void swap(Device &first, Device &second) {
using std::swap;
swap(first.property, second.property);
}
Device& operator=(Device other) {
swap(*this, other);
return *this;
}
private:
Device() = default;
unsigned property;
};
class System {
public:
explicit System(const string &name): name(name) {}
friend void swap(System &first, System &second) {
using std::swap;
swap(first.name, second.name);
swap(first.devices, second.devices);
}
System& operator=(System other) {
swap(*this, other);
return *this;
}
protected:
void addDevice(Device b1) {
devices.push_back(b1);
}
private:
vector<Device> devices;
string name;
};
class Computer: public System, public Device {
public:
Computer(unsigned property, const string& name): System(name), Device(property) {}
Computer& addAddress(const string &address) {
addresses.emplace_back(address);
return *this;
}
Computer& addComponent(Device newDevice) {
System::addDevice(newDevice); //error
return *this;
}
private:
vector<const string> addresses;
};
class Network: public System {
public:
Network(const string& name): System(name) {}
Network& addComputer(Device newComputer) {
System::addDevice(newComputer); //no error
return *this;
}
};
class CPU: public Device {
public:
CPU(unsigned coreCount, unsigned frequency): Device(coreCount), frequency(frequency) {}
private:
unsigned frequency;
};
计算机的 addComponent() 有一个错误,因为不能用 Computer 类型的表达式初始化 System 类型的对象参数
。我不完全确定这意味着什么,因为该函数是从 Computer 类调用的,它是一个设备,因此它应该可以访问父级的 addDevice()。该函数的参数是一个设备。
同样令人困惑的是我的 Network 类,其中函数 addComputer() 使用同一组指令没有错误。
谁能解释一下我的设计有何缺陷?
最佳答案
今天,当我正在编写派生类的代码时,这条错误消息突然出现,在我的例子中,关键是派生类——就在那一刻——未实例化。1
一旦我在派生类上取得了足够的进展以实例化它,错误就消失了。
您正在键入以调用基类的派生调用当前不可实例化。
考虑以下具有两个继承关系的代码。
class Type {
public:
Type(){}
virtual ~Type() {}
virtual void m() = 0;
};
class SubType: public Type {
public:
SubType(): Type() {}
virtual ~SubType() {}
// virtual void m() override; // <== Important code commented out here
};
// void SubType::m() {} // <== and here
class Base {
public:
Base() {};
virtual ~Base() {}
virtual void m();
};
class Derived: public Base, public Type {
public:
Derived(): Base() {}
virtual ~Derived() override {}
virtual void m() override;
protected:
SubType m_troublesome; // <== Note member here has incomplete type when
// comments are in place
};
void Base::m() {}
void Derived::m() {
Base::m(); // <== Tricky error message on this line
}
尝试用
编译它$ clang --std=c++11 -Wall -pedantic -c test.cc
你会收到反馈
test.cc:34:13: error: field type 'SubType' is an abstract class
SubType m_troublesome;
^
test.cc:8:18: note: unimplemented pure virtual method 'm' in 'SubType'
virtual void m() = 0;
^
test.cc:42:11: error: cannot initialize object parameter of type 'Base' with an
expression of type 'Derived'
Base::m();
如果你同时看到这三个,你就知道从哪里开始了,但是如果你使用 clang 作为 linter(就像许多 IDE 所做的那样),那么你可能会看到第三个(出现在 Base::m();
,独立于前两个的上下文中完全有效的一段代码。
瞬间困惑。
如果您从代码的两个标记部分中删除注释(使 SubType
完整,从而可以实例化 Derived
objects),代码将编译干净。
SubType
的模拟完成一半,就不会出现这种情况。1 特别是我声明了一个成员,其中我还没有覆盖继承的抽象虚方法。
关于c++ - 无法使用类型为 'Parent' 的表达式初始化类型为 'Derived' 的对象参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55856187/
我在 Ubuntu 11.10 和最新版本的 NetBeans 下使用 C++。假设我有以下代码: class Node {} class DerivedNode : public Node {} c
这两者在 GHC 中有什么区别。它们的预期用途似乎相似,但 deriving (Data)已经存在了一段时间了deriving (Generic)最近才添加到 GHC 中。 是 deriving (G
当您将矩阵对象作为 MatrixBase 引用传递给函数时会发生什么?我不明白幕后到底发生了什么。 示例函数代码如下: #include #include using namspace Eigen
我正在尝试了解如何在 Eigen 中使用样条曲线,特别是我想在某个点上找到样条插值及其一阶和二阶导数的值。找到内插值很容易,但是当我尝试计算导数时,我得到了奇怪的值。 我尝试按照手册 ( http:/
使用 C++ Builder XE7 我有一个带有 TImageList 对象的基本表单 object FormBase: TFormBase Left = 0 Top = 0 Capti
我正在制作基类和派生类。派生类的值将为 Eigen::Matrix , 并继承了 Base 的所有方法。 我这样做是为了使无论矩阵类型如何都相同的方法不会因为 Matrix 的不同模板参数而全部重复。
我最近更新到最新的 Eigen 版本 (3.3.90),看起来它破坏了我之前工作的东西(在我使用 libigl 库附带的 Eigen 版本 3.2.10 之前)。 我想将 block 的结果存储到一个
我最近一直在尝试“向我学习 Haskell”,我想创建一个新类型来表示整数状态,而不仅仅是使用原始 Integer(为了类型安全和代码清晰)。具体来说,以下代码编译: newtype AuxState
这个问题已经有答案了: Access subclass fields from a base class in Java (4 个回答) 已关闭 4 年前。 我对 Java 和面向对象编程总体来说是新
我有这些类,事件记录模式的实现: public abstract class RecordCollection : ObservableCollection where T : Record publ
首先,这个问题非常类似于 downcasting shared pointer to derived class with additional functionality is ,哪里有好的答案。但
好的,我正在通读 this entry in the FQA处理将 Derived** 转换为 Base** 的问题以及为什么它被禁止,我得到的问题是你可以分配给 Base* 不是 Derived*
我正在使用 Boost.Python 将我的 C++ 代码公开给 Python。我遇到了与将对象从一种语言多次传递到另一种语言有关的困难。这是我想要做的: C++代码 class Base { p
这个问题在这里已经有了答案: Downcasting using the 'static_cast' in C++ (3 个答案) 关闭 8 年前。 我不明白为什么会这样。pReallyABase
https://sqlzoo.net/wiki/SELECT_within_SELECT_Tutorial 的问题 7 “找到每个大陆中最大的国家(按面积),显示大陆、名称和面积:” 我不明白为什么
我很难理解为什么以下代码无法编译: template class Base { public: Base(int a){} }; template class Derive
我很难理解为什么以下代码无法编译: template class Base { public: Base(int a){} }; template class Derive
Base to Derived 是可能的(装箱)。但是 Derived to base 给出了运行时异常,然而这相当于拆箱。为什么会这样 Base b = new Base(); Child c =
库代码 我的图书馆有一个 CRTP 类 B . 我创建了一个 Trait使用户能够更改 B 行为的类. 默认设置为 int . ( #1 ) #include #include //B and T
我正在学习 C++ 继承,所以我通过动态创建一个 Base 类来尝试这段代码,并对它的 Derived 类进行向下转换(显然向下转换无效)以使这个动态创建的 Base 对象被指向派生类指针。但是当我通
我是一名优秀的程序员,十分优秀!