C++想要从基类调用派生函数
我有以下情况
class base
{
public:
virtual void Accepted(SOCKET s) //// Event
{
}
void Listner()
{
SOCKET acpted;
Accepted(acpted); /// When I call I want derived class's Accepted() to get called
}
};
class derived
{
virtual void Accepted(SOCKET s) //// Event
{
////// HERE i will write actual implementation (QUESTION)
}
}
我想调用派生类的函数。这将像这里的事件一样工作。我想通知派生类基类中发生了一些事情。
I have following situation
class base
{
public:
virtual void Accepted(SOCKET s) //// Event
{
}
void Listner()
{
SOCKET acpted;
Accepted(acpted); /// When I call I want derived class's Accepted() to get called
}
};
class derived
{
virtual void Accepted(SOCKET s) //// Event
{
////// HERE i will write actual implementation (QUESTION)
}
}
I want to call derived class's function. This will work like event here. I want to notify derived class that something happened in base class.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
class衍生:公共基
将使衍生
实际上继承自base
。然后你的虚拟函数调用将按预期工作。请注意,您不能在
base
的构造函数或析构函数中进行此类虚函数调用 - 在调用base
的构造函数时,派生
部分尚不存在。在调用base
析构函数时,衍生
部分已经被析构。编辑:演示,回应评论。
这将打印
衍生::接受
class derived : public base
will makederived
actually inherit frombase
. Then your virtual function call will work as expected.Note that you cannot make such virtual function calls in the constructor or destructor of
base
- At the timebase
's constructor is invoked, thederived
part doesn't yet exist. At the timebase
destructor is invoked, thederived
part has already been destructed.EDIT: Demo, in response to comment.
This will print
derived::Accepted
这按照你想要的方式工作。如果您的实际代码无法正常工作,那么您还没有向我们展示所有相关内容。
以下是您可以如何做您想做的事情,也是您应该如何发帖的示例。
输出是:
This works the way you want. If your actual code isn't working, then you haven't shown us everything relevant.
Here is how you can do what you're trying to do, and also an example of how you should be posting.
Output is:
除了使派生类扩展基类(派生:公共基类)之外,您还可以在基类中声明您接受的方法为纯虚拟:
这样派生类就被迫实现接受的方法。
而且派生类中的 Accepted 方法不需要是虚拟的。
In addition to make de derived class extending the base class (derived : public base) you could also declare you Accepted method pure virtual in the base class:
this way the derived class is forced to implement the accepted method.
And also the Accepted method in the derived class doesn't need to be virtual.
在您的代码中进行以下更改。
1) 派生类:公共基类
2)创建派生类的对象并调用Listener函数。
(如果您正在创建基类的对象,它将不起作用)
Dhiraj
Make following changes in your code.
1) class derived : public base
2) Create object of derived class and call Listener function.
(If you are creating object of base class, it will not work)
Dhiraj
请注意不要在构造或销毁期间调用虚拟函数。错误的实现定义的事情可能会发生:
构造函数中的 C++ 虚函数
Be careful not to call your virtual functions during construction or destruction. Bad implementation defined things can happen:
C++ virtual function from constructor
谢谢朋友的解答。所有这些在技术上都是正确的。
方式解决了这个问题
我按照我想通过不将任何方法设置为纯虚拟来允许创建类基对象的
。如果我将任何方法设为纯虚拟,那么它会使类基成为抽象的。
因此,我创建了另一个抽象类,其中所有必需的方法都是纯虚拟的,并且我从中派生类基。这样我就可以创建基础对象。
例如
Thanks friend for your answers. All were technically correct.
I solved it following way
I wanted to allow creating object of class base by not making any of the method as pure virtual.
If i make any of the method as pure virtual then it makes class base as abstract.
So i create another abstract class in which all required methods are pure virtual and i am deriving class base from that. that way it allowed me to create object of base.
e.g.