C++想要从基类调用派生函数

发布于 2024-10-28 18:06:49 字数 482 浏览 9 评论 0原文

我有以下情况

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

路弥 2024-11-04 18:06:49

class衍生:公共基将使衍生实际上继承自base。然后你的虚拟函数调用将按预期工作。

请注意,您不能在 base 的构造函数或析构函数中进行此类虚函数调用 - 在调用 base 的构造函数时,派生部分尚不存在。在调用base析构函数时,衍生部分已经被析构。

编辑:演示,回应评论。

class base
{
public:
    virtual void Accepted(SOCKET s)  //// Event
    {
        cout << "base::Accepted" << endl;
    }
    void Listner()
    {
        SOCKET acpted = 0;
        Accepted(acpted); /// When I call I want derived class's Accepted() to get called
    }
};


class derived : public base
{
    virtual void Accepted(SOCKET s)  //// Event
    {
        cout << "derived::Accepted" << endl;
    }
};


int main(int argc, char * argv[])
{
  derived d;
    d.Listner();
}

这将打印衍生::接受

class derived : public base will make derived actually inherit from base. 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 time base's constructor is invoked, the derived part doesn't yet exist. At the time base destructor is invoked, the derived part has already been destructed.

EDIT: Demo, in response to comment.

class base
{
public:
    virtual void Accepted(SOCKET s)  //// Event
    {
        cout << "base::Accepted" << endl;
    }
    void Listner()
    {
        SOCKET acpted = 0;
        Accepted(acpted); /// When I call I want derived class's Accepted() to get called
    }
};


class derived : public base
{
    virtual void Accepted(SOCKET s)  //// Event
    {
        cout << "derived::Accepted" << endl;
    }
};


int main(int argc, char * argv[])
{
  derived d;
    d.Listner();
}

This will print derived::Accepted

孤独岁月 2024-11-04 18:06:49

这按照你想要的方式工作。如果您的实际代码无法正常工作,那么您还没有向我们展示所有相关内容。

以下是您可以如何做您想做的事情,也是您应该如何发帖的示例。

#include <iostream>
using namespace std;

class Base
{
public:
    virtual void Accepted()
    {
        cout << "Base::Accepted" << endl;
    }
    void Listener()
    {
        cout << "Base::Listener" << endl;
        Accepted();
    }
};

class Der : public Base
{
public:
    void Accepted()
    {
        cout << "Derived::Accepted" << endl;
    }
};

int main()
{
    cout << "*** BASE ***" << endl;
    Base b;
    b.Listener();

    cout << "\n*** DERIVED ***" << endl;
    Der d;
    d.Listener();
}

输出是:

*** BASE ***
Base::Listener
Base::Accepted

*** DERIVED ***
Base::Listener
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.

#include <iostream>
using namespace std;

class Base
{
public:
    virtual void Accepted()
    {
        cout << "Base::Accepted" << endl;
    }
    void Listener()
    {
        cout << "Base::Listener" << endl;
        Accepted();
    }
};

class Der : public Base
{
public:
    void Accepted()
    {
        cout << "Derived::Accepted" << endl;
    }
};

int main()
{
    cout << "*** BASE ***" << endl;
    Base b;
    b.Listener();

    cout << "\n*** DERIVED ***" << endl;
    Der d;
    d.Listener();
}

Output is:

*** BASE ***
Base::Listener
Base::Accepted

*** DERIVED ***
Base::Listener
Derived::Accepted
埋葬我深情 2024-11-04 18:06:49

除了使派生类扩展基类(派生:公共基类)之外,您还可以在基类中声明您接受的方法为纯虚拟:

virtual void Accepted(SOCKET s) = 0;

这样派生类就被迫实现接受的方法。

而且派生类中的 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:

virtual void Accepted(SOCKET s) = 0;

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.

旧夏天 2024-11-04 18:06:49

在您的代码中进行以下更改。
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

爱人如己 2024-11-04 18:06:49

请注意不要在构造或销毁期间调用虚拟函数。错误的实现定义的事情可能会发生:

构造函数中的 C++ 虚函数

Be careful not to call your virtual functions during construction or destruction. Bad implementation defined things can happen:

C++ virtual function from constructor

眉目亦如画i 2024-11-04 18:06:49

谢谢朋友的解答。所有这些在技术上都是正确的。
方式解决了这个问题

我按照我想通过不将任何方法设置为纯虚拟来允许创建类基对象的

。如果我将任何方法设为纯虚拟,那么它会使类基成为抽象的。

因此,我创建了另一个抽象类,其中所有必需的方法都是纯虚拟的,并且我从中派生类基。这样我就可以创建基础对象。

例如

class __base__
{
public:
   virtual void Accepted(SOCKET s)  = 0 //// Event    
   {     
   }    

};

class base : public __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 : public base
{    
  virtual void Accepted(SOCKET s)  //// Event    
  {          
      ////// HERE i will write actual implementation (QUESTION)    
  }  
}

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.

class __base__
{
public:
   virtual void Accepted(SOCKET s)  = 0 //// Event    
   {     
   }    

};

class base : public __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 : public base
{    
  virtual void Accepted(SOCKET s)  //// Event    
  {          
      ////// HERE i will write actual implementation (QUESTION)    
  }  
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文