是否有可能取代在运行时的方法在C / C ++? [英] Is it possible to replace a method at runtime in C/C++?

查看:97
本文介绍了是否有可能取代在运行时的方法在C / C ++?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想和能力的插件系统覆盖在运行时的方法。

I want to make a plugin system with the capability to override a method at runtime.

一些答案​​说函数指针,但如何定义的函数或类?

Some answers say function pointers, but how about a defined function or class?

这样的:

class foo
{
  public:
    bar(int foobar);
}

有没有一种方法来获取该函数指针,或者更换呢?

Is there a way to get function pointer for that, or replace it?

BTW;请不要说挂钩,因为它太特定于平台的和危险的。
如果你想说钩,请你只说不可能。

BTW; please don't say hook since it's too platform specific and dangerous. If you want to say hook, please just say IMPOSSIBLE.

推荐答案

运行功能替代品可以用的几种技术之一来实现:

Runtime function "replacement" can be achieved with one of several techniques:


  1. 多态性

  2. 标准库设施,如 STD ::功能

  3. 第三方库或平台特定的技术来翻译或派遣函数调用。

在哪个选项是更好是高度依赖于预期的用途和目标环境。例如;插件系统很可能利用多态性(用适当的工厂,甚至可能与模板方法模式),而内部功能路由可以使用的std ::功能

On which option is better is highly dependent on the intended use and target environments. For example; plugin systems could well make use of polymorphism (with the appropriate factories and possibly even combined with the template method pattern) whilst internal function routing could use std::function.

这些技术不会真正替换任何功能,而是可以在运行时根据需要设置路由功能调用。

These techniques wouldn't really "replace" any of the functions, but rather can be set up at runtime to route the function call as required.

注意的我专注于问题的C ++方面的问题(它被标记C和C ++,但样品code是C ++)。

Note I've focused on the C++ aspects of the question (it was tagged C and C++ but the sample code is C++).

这篇关于是否有可能取代在运行时的方法在C / C ++?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆