简体   繁体   English

C ++纯虚函数和python中的此指针

[英]C++ pure virtual functions and this pointer in python

I'm a former c++ coder and have made the plunge into python for several months, which has been great. 我曾经是c ++程序员,并且已经投入python了几个月,这真是太好了。 I'm doing a bit of code porting and have come across a problem of which i'm not sure the best path. 我正在做一些代码移植,遇到一个我不确定最佳路径的问题。 There are many ways to skin a cat - but i'm looking for advice on what would be the 'best' and/or most pythonic way to do something similar to the section of c++ code below. 有很多方法可以给猫做皮毛-但是我正在寻找有关执行与下面的c ++代码部分相似的操作的“最佳”和/或最pythonic方法的建议。

I've cut the code to a trivial 'reproducer' just to highlight what was happening. 我将代码剪切为琐碎的“复制器”,只是为了强调正在发生的事情。 Essentially there is a well-defined interface of callbacks the server will call when certain events happen. 本质上,当某些事件发生时,服务器将调用一个定义明确的回调接口。 When a server is created, it is provided a callback interface as an argument. 创建服务器时,将为其提供一个回调接口作为参数。

In the below case, the client has implemented these callbacks on itself, and thus when it is creating the server, it provides the *this pointer. 在以下情况下,客户端已在其自身上实现了这些回调,因此在创建服务器时,它将提供* this指针。

Is this something similar in python? 这与python类似吗? Any suggestions would get greatly appreciated. 任何建议将不胜感激。

#include <iostream>

// Client Feed Handler function Interface
class ClientFeedHandlersI
{
public:
    virtual void HandlerFeed1() = 0;
    virtual void HandlerFeed2() = 0;
};

// A very basic server
class Server
{
public:
    Server(ClientFeedHandlersI& handlers) 
        : mHandlers(handlers) {}

void Start()
{
    // Create some random events to illustrate
    for (int i = 0; i < 10; ++i)
        EventLoopHandler();
}
private:
    void EventLoopHandler()
    {
        if (rand() % 10 > 5)
            mHandlers.HandlerFeed1();
        else
            mHandlers.HandlerFeed2();
    }

    ClientFeedHandlersI& mHandlers;
};

// A really simple client
class Client : private ClientFeedHandlersI
{
public:
    Client() 
      : mMyServer(*this)
    {
        mMyServer.Start();
    }

private:
    void HandlerFeed1() override { std::cout << "HandlerFeed1 called\n"; }
    void HandlerFeed2() override { std::cout << "HandlerFeed2 called\n"; }

    Server mMyServer;
};

int main()
{
    auto c = Client();
}

So here's an attempt at porting to python. 因此,这是尝试移植到python的尝试。 Note that in the real world example, there are 20+ client feed handler functions, hence why i want to force the interface using the @abstractmethod. 请注意,在实际示例中,有20多个客户端供稿处理程序函数,因此为什么我要使用@abstractmethod强制使用该接口。

# python 3.6
from abc import ABC, abstractmethod

class ClientFeedHandlersI(ABC):

    def __init__(self):
        pass

    @abstractmethod
    def handler_feed_1(self):
        pass

   @abstractmethod
   def handler_feed_2(self):
        pass

class Server:

    def __init__(self, clientCallBacks:ClientFeedHandlersI):

        self.clientCallBacks = clientCallBacks

    def start(self):
        for ii in range(10):
            self.event_loop_handler()

    def event_loop_handler(self):
        import random
        if random.randint(0,10) > 5:
            self.clientCallBacks.handler_feed_1()
        else:
            self.clientCallBacks.handler_feed_2()


class Client(ClientFeedHandlersI):

    def __init__(self):

        self.server = Server(self)
        self.server.start()

    def handler_feed_1(self):
        print("HandlerFeed1 called\n")
    def handler_feed_2(self):
        print("HandlerFeed2 called\n")

if __name__ == '__main__':
    c = Client()

Edit: The above code sample now works as per the c++ code. 编辑:上面的代码示例现在按c ++代码工作。

OP中C ++以下的Python示例代码现在可以使用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM