功能指针的优点/缺点 [英] Advantage/Disadvantage of function pointers

查看:118
本文介绍了功能指针的优点/缺点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我遇到的问题实际上尚未发生.我正在为当前正在开发的游戏计划一些代码,并且我知道从第一步开始,我将需要尽可能地节省内存使用.

我的问题是,例如,如果我有500k个对象,则需要不断对其进行构造和解构.

例如,将为我节省所有内存以具有那些类将用作功能指针的功能.没有函数指针
class MyClass
{
public:
void Update();
void Draw();
...
}

>例如具有函数指针的

class MyClass
{
public:
void * Update;
void * Draw; ...
}

这是否会为我节省任何内存,还是新创建的MyClass只会访问为其其余部分定义的相同功能?如果确实为我节省了任何记忆,那么值得吗?

So the problem I am having has not actually happened yet. I am planning out some code for a game I am currently working on and I know that I am going to be needing to conserve memory usage as much as possible from step one.

My question is, if I have for example, 500k objects that will need to constantly be constructed and deconstructed. Would is save me any memory to have the functions those classes are going to use as function pointers.

e.g. without function pointers
class MyClass
{
public:
void Update();
void Draw();
...
}

e.g. with function pointers

class MyClass
{
public:
void * Update;
void * Draw;
...
}

Would this save me any memory or would any new creation of MyClass just access the same functions that were defined for the rest of them? If it does save me any memory would it be enough to be worthwhile?

推荐答案

假设这些不是虚拟函数,则可以将 more 内存与函数指针一起使用.

Assuming those are not virtual functions, you'd use more memory with function pointers.

第一个示例

没有分配(超出使new返回唯一指针所需的基本数量,或者...省略了的其他实现).

There is no allocation (beyond the base amount required to make new return unique pointers, or your additional implementation that you ... ellipsized).

非虚拟成员函数基本上是带有this指针的静态函数.

Non-virtual member functions are basically static functions taking a this pointer.

优点是您的对象非常简单,并且只有一个地方可以查找对应的代码.

The advantage is that your objects are really simple, and you'll have only one place to look to find the corresponding code.

缺点是您失去了一些灵活性,必须将所有代码都塞入单个更新/绘制函数中.除了一个小游戏,这将很难管理.

The disadvantage is that you lose some flexibility, and have to cram all your code into single update/draw functions. This will be hard to manage for anything but a tiny game.

第二个示例

您为每个对象实例分配2个指针.指针通常每个都是4x到8x字节(取决于目标平台和编译器).

You allocate 2x pointers per object instance. Pointers are usually 4x to 8x bytes each (depending on your target platform and your compiler).

您将获得很大的灵活性,即能够在运行时更改指向的功能,并具有实现该功能的众多功能-因此可提供支持(但不能保证)更好的代码组织.

The advantage you gain a lot of flexibility of being able to change the function you're pointing to at runtime, and can have a multitude of functions that implement it - thus supporting (but not guaranteeing) better code organization.

缺点是,在调试应用程序时或仅在阅读代码时,将很难确定每个实例将指向哪个函数.

The disadvantage is that it will be harder to tell which function each instance will point to when you're debugging your application, or when you're simply reading through the code.

其他选项

以这种特定方式使用函数指针(实例数据成员)在普通C语言中通常更有意义,而在C ++中则更没有意义.

Using function pointers this specific way (instance data members) usually makes more sense in plain C, and less sense in C++.

如果希望在运行时绑定这些功能,则可能希望将其设置为virtual.成本取决于编译器/实现,但是我相信(通常)每个对象实例将使用一个v表指针.

If you want those functions to be bound at runtime, you may want to make them virtual instead. The cost is compiler/implementation dependent, but I believe it is (usually) going to be one v-table pointer per object instance.

此外,您可以充分利用虚函数语法来使函数基于类型,而不是将其绑定到任何类型.比起函数指针选项,调试起来要容易得多,因为您可以简单地查看派生类型,以找出特定实例所指向的函数.

Plus you can take full advantage of virtual function syntax to make your functions based on type, rather than whatever you bound them to. It will be easier to debug than the function pointer option, since you can simply look at the derived type to figure out what function a particular instance points to.

您也不必初始化那些函数指针-C ++类型系统将自动进行等效的初始化(通过构建v表,并初始化每个实例的v表指针).

You also won't have to initialize those function pointers - the C++ type system would do the equivalent initialization automatically (by building the v-table, and initializing each instance's v-table pointer).

请参阅: http://www.parashift.com/c++- faq-lite/virtual-functions.html

这篇关于功能指针的优点/缺点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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