c++ - 使用桥接模式 C++ 实现复制构造函数

标签 c++ design-patterns constructor bridge

我正在学习 C++ 并尝试实现桥接模式,发生这种情况时,我有带有构造函数的实现文件:

 SystemImpl::SystemImpl() {
    this->name = "";
    this->value = 0.0;
    this->maxValue = DBL_MAX;
}

SystemImpl::SystemImpl(const SystemImpl& sys) {
    this->name = sys.name;
    this->value = sys.value;
    this->maxValue = sys.maxValue;
}

现在,我正在创建使用此实现的接口(interface),其中 imps 是我指向实现类的指针:

System::System() {
    imps = new SystemImpl();
}

System::System(const System& sys) {
    imps = new SystemImpl(sys);
}

第一个构造函数工作正常,但第二个是复制构造函数,显示 没有用于调用“SystemImpl::SystemImpl(const System&)”的匹配函数

怎么了?

最佳答案

对于 imps = new SystemImpl(sys);,编译器提示 SystemImpl 没有以 System 作为参数的构造函数.

你可能想要

System::System(const System& sys) {
    imps = new SystemImpl(*sys.imps);
}

关于c++ - 使用桥接模式 C++ 实现复制构造函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35592725/

相关文章:

class - 何时需要使用 new 初始化 F# 类型?

C++对`vtable的 undefined reference

c++ - 您是否使用过任何 C++ 解释器(不是编译器)?

c++ - 尝试为 Windows C++ 创建 Gatt 客户端应用程序,该应用程序在建立连接时不会失败

c++ - 反转 18 位 int 的二进制补码

java - robocode引擎: how to design (write) the runtime engine -- the robot world

javascript - 在 Node.js 插件中使用 std::thread

database - 如何正确实现存储库模式?

node.js - 数据库更新后nodejs自动刷新 View

c++ - 如何使用 new(std::nothrow) 使构造函数失败?