Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/125.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 如何从继承的父类创建唯一的_ptr_C++_Inheritance_Unique Ptr - Fatal编程技术网

C++ 如何从继承的父类创建唯一的_ptr

C++ 如何从继承的父类创建唯一的_ptr,c++,inheritance,unique-ptr,C++,Inheritance,Unique Ptr,有没有办法从一个继承的类创建一个唯一的_ptr 我需要能够向经理注册MouseListener,但我不知道如何创建继承的MouseListener的唯一\u ptr 错误是找不到从Window*到MouseListener的转换。我尝试过静态_转换,但这会产生其他错误。我还尝试传入一个指向addMouseListener的原始指针,该指针确实有效,但在关闭程序时出错,因为我认为它没有创建适当的内存,导致删除失败 还可以使用std::move传输所有权,这会导致侦听器不触发事件 // Window

有没有办法从一个继承的类创建一个唯一的_ptr

我需要能够向经理注册MouseListener,但我不知道如何创建继承的MouseListener的唯一\u ptr

错误是找不到从Window*到MouseListener的转换。我尝试过静态_转换,但这会产生其他错误。我还尝试传入一个指向addMouseListener的原始指针,该指针确实有效,但在关闭程序时出错,因为我认为它没有创建适当的内存,导致删除失败

还可以使用std::move传输所有权,这会导致侦听器不触发事件

// Window.h
class Window : public MouseManager, public MouseListener {
public:
    Window::Window(std::string title, int32_t width, int32_t height) {
        ...
        this->addMouseListener(std::make_unique<MouseListener>(this)); // ERROR
    }
};

// MouseManager.h
void MouseManager::addMouseListener(std::unique_ptr<MouseListener> listener) {
    m_listeners.emplace_back(listener);
}

// MouseListener.h

MouseListener() = default;

virtual ~MouseListener() = default;
MouseListener(const MouseListener& listener) = default;
MouseListener(MouseListener&& listener) noexcept ;
MouseListener& operator=(const MouseListener& listener) = delete;
MouseListener& operator=(MouseListener&& listener) = delete;
错误输出

In file included from /Users/Programmer/CLionProjects/StormEngine/Engine/Window/Window.cpp:5:
In file included from /Users/Programmer/CLionProjects/StormEngine/Engine/Window/Window.h:8:
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/memory:3141:32: error: no matching constructor for initialization of 'MouseListener'
    return unique_ptr<_Tp>(new _Tp(_VSTD::forward<_Args>(__args)...));
                               ^   ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/Users/Programmer/CLionProjects/StormEngine/Engine/Window/Window.cpp:17:33: note: in instantiation of function template specialization 'std::__1::make_unique<MouseListener, Window *>' requested here
    this->addMouseListener(std::make_unique<MouseListener>(this));
                                ^
/Users/Programmer/CLionProjects/StormEngine/Engine/Window/../Events/Listeners/MouseListener.h:19:5: note: candidate constructor not viable: no known conversion from 'Window *' to 'const MouseListener' for 1st argument; dereference the argument with *
    MouseListener(const MouseListener& listener) = default;
    ^
/Users/Programmer/CLionProjects/StormEngine/Engine/Window/../Events/Listeners/MouseListener.h:20:5: note: candidate constructor not viable: no known conversion from 'Window *' to 'MouseListener' for 1st argument; dereference the argument with *
    MouseListener(MouseListener&& listener) noexcept ;
    ^
/Users/Programmer/CLionProjects/StormEngine/Engine/Window/../Events/Listeners/MouseListener.h:16:5: note: candidate constructor not viable: requires 0 arguments, but 1 was provided
    MouseListener() = default;
    ^
更新:


取消引用这会增加一个新问题,即在何处将变量listener添加到std::move,这会导致所有权的更改,如上所述,这会导致事件不触发。

使唯一性生成该类型的对象并返回指向该对象的指针

它不会将预先存在的指向对象的指针包装到唯一的ptr中

您的addMouseListenerstd::unique_ptr函数拥有侦听器的所有权。在这里传递预退出对象通常不是一个好主意

可能窗口不应该继承MouseListener,而是创建一个MouseListener,该MouseListener反过来有一个指向窗口的指针,并以某种方式进行生存期管理,以确保窗口保持活动状态,或者在窗口关闭时断开连接

鼠标侦听器应该将消息中继到窗口,而不是窗口

此指针可以传递给MouseManager::addMouseListener。请注意,虽然派生唯一指针的转换是隐式的,但不能直接从裸派生指针构造基唯一指针,因为构造函数是显式的


请注意,创建指向此的唯一指针可能是一个可疑的想法。它将窗口对象的创建限制为使用new,显然是从分配指针的代码的角度来看。除非从最初存储窗口的容器中移出,否则其他代码不能拥有任何窗口,并且您永远不能拥有自动实例。

1。你看到错误信息了吗?2.创建。我指定了此实现的错误。它找不到从Window*到MouseListener的转换。我将发布错误日志,以防人们错过。MouseListener的构造函数是什么?所有三个都显示在错误日志中。如果需要的话,我可以把这个添加到问题中。@Matthew您需要添加所有类型的定义。但是不要使用不需要的类型来演示问题。如果不需要的话,也可以删除部分定义。换句话说,创建一个。所以,如果我理解正确,从MouseListener继承窗口是可以的,但使用全局MouseManager之类的东西?从MouseListener继承窗口违反了单一责任原则。使用组合可能是一种不那么难闻的设计。好吧,在阅读了组合之后,你似乎在说在Window类中使用可变MouseListener是个好主意?
std::make_unique<MouseListener>(this)
std::unique_ptr<Window>(this)