关于多态性的几点思考 [英] Some thoughts on polymorphism

查看:74
本文介绍了关于多态性的几点思考的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经用C ++编程了两年多一点,而且我仍然想知道我什么时候应该使用多态性。


有些人声称多态性是C ++中不可或缺的一部分,

任何不使用它的人都可能只是简单地编程

C.我完全不同意有了这个,因为我认为C ++除了多态之外还有很多很棒的功能,例如能够将代码组织成类,通过继承代码重用,模板,

模板专业化等等。


尽管如此,大多数人都会因为

多态性而推广C ++和OO。但出于某种原因,我发现定义很多

从基类继承的小类的想法有点过于夸张只是为了做一些基本上归结的事情到一个

函数指针。


采用以下示例:


最近我正在开展一个项目服务器会解释客户端发出的各种命令

。有大约25种不同的

可能的命令。客户端发出的每个命令都应该导致

服务器以不同的方式响应。


那么在C ++中实现它的最佳方法是什么?


首先想到的是一种典型的C方法:制作一个关键字(命令)的哈希表,并将每个密钥与一个密钥相关联

函数指针。这很简单,效率极高。但是,自从我用C ++编程后,我觉得有必要去探索更多的OOish

替代方案。另外,服务器程序本身就是一个类,所以我要
需要让哈希表存储指向函数的指针,这些函数不是这个类的一部分,打破了整个设计。 (除非我

使用可怕的成员函数指针 - __nobody__曾经使用过,并且

这需要相当大的开销才能解除引用。)


所以,我想:多态性怎么样?我可以创建一个抽象的

基类命令,只需一个虚函数,然后生成25个/或
从它继承的小类。然后我就可以有一个

泛型execute()函数,如:


void execute_command(Command * c)

{

c-> dosomething();

}


这似乎是执行此操作的标准C ++方式。

尽管如此,有些东西看起来很荒谬。我

定义了25个类,所以我可以动态地在函数之间进行选择。

最重要的是,这是老式的方式(一张普通的表格)
函数指针)效率更高,因为它不涉及

vtable。


出于某种原因,我不是感觉像C ++提供了一个优雅的解决方案来解决这个问题。我只是太挑剔了吗?或者有人同意吗?

评论,想法,建议,将不胜感激。

I''ve been programming in C++ for a little over 2 years, and I still
find myself wondering when I should use polymorphism.

Some people claim that polymorphism is such an integral part of C++,
that anybody who doesn''t use it might as well just program in plain
C. I totally disagree with this, because I think C++ has a lot of
great features apart from polymorphism, such as the ability to
organize code into classes, code reuse through inheritance, templates,
template specialization, etc.

Still, most people promote C++ and OO in general because of
polymorphism. But for some reason, I find the idea of defining lots
of little classes that inherit from a base class to be a bit
overreaching just to do something that essentially boils down to a
function pointer.

Take the following example:

Recently I was working on a project where a server would interpret
various commands issued by a client. There were about 25 different
possible commands. Each command the client issues should cause the
server to respond in a different way.

So what''s the best way to implement this in C++?

The first thing that came to mind was a typical C approach: make a
hash table of key words (commands), and associate each key with a
function pointer. This is simple and extremely efficient. But since
I''m programming in C++ I feel compelled to explore more OOish
alternatives. Also, the server program itself was a class, so I''d
need to have the hash table store pointers to functions which are not
part of the class, which kind of breaks the whole design. (Unless I
use dreaded member function pointers - which __nobody__ ever uses, and
which entail considerable overhead to dereference.)

So, I thought: how about polymorphism? I could create an abstract
base class "command", with a single virtual function, and then make 25
little classes which inherit from it. Then I could just have a
generic execute() function, like:

void execute_command(Command* c)
{
c->dosomething();
}

This seems like it would be the standard C++ way of doing this.
Still, there''s something about it that just seems ridiculous. I
define 25 classes, just so I can pick between functions dynamically.
On top of that, doing this the old fashioned way (a table of plain
function pointers) is more efficient, since it doesn''t involve the
vtable.

For some reason, I don''t feel like C++ offers an elegant solution to
this problem. Am I just being too picky? Or does anyone agree?
Comments, thoughts, suggestions, would be appreciated.

推荐答案

< ch******@gmail.com在留言中写道

news:11 ********************** @ y5g2000hsa.googlegro ups.com ...
<ch******@gmail.comwrote in message
news:11**********************@y5g2000hsa.googlegro ups.com...

我已经用C ++编程了2年多一点,我仍然会发现自己我想知道何时应该使用多态。


有些人声称多态性是C ++中不可或缺的一部分,

任何不使用它的人都可能以及简单的程序

C.我完全不同意这一点,因为我认为C ++除了多态之外还有很多很棒的功能,比如能够

将代码组织成类,通过继承代码重用,模板,

模板专业化等等。


尽管如此,大多数人都在推广C ++和OO一般是因为

多态性。但出于某种原因,我发现定义很多

从基类继承的小类的想法有点过于夸张只是为了做一些基本上归结的事情到一个

函数指针。


采用以下示例:


最近我正在开展一个项目服务器会解释客户端发出的各种命令

。有大约25种不同的

可能的命令。客户端发出的每个命令都应该导致

服务器以不同的方式响应。


那么在C ++中实现它的最佳方法是什么?


首先想到的是一种典型的C方法:制作一个关键字(命令)的哈希表,并将每个密钥与一个密钥相关联

函数指针。这很简单,效率极高。但是,自从我用C ++编程后,我觉得有必要去探索更多的OOish

替代方案。另外,服务器程序本身就是一个类,所以我要
需要让哈希表存储指向函数的指针,这些函数不是这个类的一部分,打破了整个设计。 (除非我

使用可怕的成员函数指针 - __nobody__曾经使用过,并且

这需要相当大的开销才能解除引用。)


所以,我想:多态性怎么样?我可以创建一个抽象的

基类命令,只需一个虚函数,然后生成25个/或
从它继承的小类。然后我就可以有一个

泛型execute()函数,如:


void execute_command(Command * c)

{

c-> dosomething();

}


这似乎是执行此操作的标准C ++方式。

尽管如此,有些东西看起来很荒谬。我

定义了25个类,所以我可以动态地在函数之间进行选择。

最重要的是,这是老式的方式(一张普通的表格)
函数指针)效率更高,因为它不涉及

vtable。


出于某种原因,我不是感觉像C ++提供了一个优雅的解决方案来解决这个问题。我只是太挑剔了吗?或者有人同意吗?

评论,想法,建议,将不胜感激。
I''ve been programming in C++ for a little over 2 years, and I still
find myself wondering when I should use polymorphism.

Some people claim that polymorphism is such an integral part of C++,
that anybody who doesn''t use it might as well just program in plain
C. I totally disagree with this, because I think C++ has a lot of
great features apart from polymorphism, such as the ability to
organize code into classes, code reuse through inheritance, templates,
template specialization, etc.

Still, most people promote C++ and OO in general because of
polymorphism. But for some reason, I find the idea of defining lots
of little classes that inherit from a base class to be a bit
overreaching just to do something that essentially boils down to a
function pointer.

Take the following example:

Recently I was working on a project where a server would interpret
various commands issued by a client. There were about 25 different
possible commands. Each command the client issues should cause the
server to respond in a different way.

So what''s the best way to implement this in C++?

The first thing that came to mind was a typical C approach: make a
hash table of key words (commands), and associate each key with a
function pointer. This is simple and extremely efficient. But since
I''m programming in C++ I feel compelled to explore more OOish
alternatives. Also, the server program itself was a class, so I''d
need to have the hash table store pointers to functions which are not
part of the class, which kind of breaks the whole design. (Unless I
use dreaded member function pointers - which __nobody__ ever uses, and
which entail considerable overhead to dereference.)

So, I thought: how about polymorphism? I could create an abstract
base class "command", with a single virtual function, and then make 25
little classes which inherit from it. Then I could just have a
generic execute() function, like:

void execute_command(Command* c)
{
c->dosomething();
}

This seems like it would be the standard C++ way of doing this.
Still, there''s something about it that just seems ridiculous. I
define 25 classes, just so I can pick between functions dynamically.
On top of that, doing this the old fashioned way (a table of plain
function pointers) is more efficient, since it doesn''t involve the
vtable.

For some reason, I don''t feel like C++ offers an elegant solution to
this problem. Am I just being too picky? Or does anyone agree?
Comments, thoughts, suggestions, would be appreciated.



你所描述的对于多态性来说听起来并不是很好用

必然。有很多地方可以使用多态,而且很多地方都没有使用它。


比如说,我是一个游戏写作。客户端将命令发送到

服务器。我用几乎C语言解释命令。但是,这些

命令使用OOP处理对象,因为角色是一个对象,它可以是玩家或NPC(非玩家角色)。这就是

多态性的来源。


我通常发现我想要使用多态的时间是我想要的时候

可以是不同类型,玩家或NPC的物品的容器。

What you describe doesn''t sound like a good use for polymorphism
necessarily. There are a lot of places to use polymorphism, and a lot of
places not to use it.

Take, for instance, a game I''m writing. The clients sent commands to the
server. I interpret the commands in pretty much a C way. However, these
commands work on objects using OOPs, as a character is an object which can
be either a player or an NPC (non player character). Which is where the
polymorphism comes in.

The time I usually find I want to use polymorphism is when I want a
container of objects that can be of different types, players or NPCs.


多年来我发现人们在这个问题上争论很多很多

次。人们谈论程序编程,OO,Generic

编程等。不同的人持有不同的信仰,并且用于他们自己的编程风格。你可以争论

年,而不是结果。


我的观点是,对于简单的问题,C风格的程序模型。适合我

最好的。你提到的问题属于简单的情况。对于

大型复杂系统,OO模型提供了系统的DESIGN

方法,这里的关键字是DESIGN,而不是实现效率。


PQ

Over the years I''ve found people arguable on this issue many, many
times. People talk about procedural programming, OO, Generic
Programming, etc. Different people holds different beliefs and are
used to their own style of programming. You can argue about these for
years and not have a result.

My view is, for simple problems, C style "procedural model" suits me
the best. The problem you mentioned belongs to the simple case. For
large and complex systems, the OO model offers a systematic DESIGN
approach, the keyword here is "DESIGN", not implementation efficiency.

Regards,

PQ




< ch******@gmail.com在留言中写道

news:11 ********************** @ y5g2000hsa.googlegro ups.com ...

<ch******@gmail.comwrote in message
news:11**********************@y5g2000hsa.googlegro ups.com...

采取以下示例:


最近我正在开发一个服务器的项目解释

客户发出的各种命令。有大约25种不同的

可能的命令。客户端发出的每个命令都应该导致

服务器以不同的方式响应。


那么在C ++中实现它的最佳方法是什么?
Take the following example:

Recently I was working on a project where a server would interpret
various commands issued by a client. There were about 25 different
possible commands. Each command the client issues should cause the
server to respond in a different way.

So what''s the best way to implement this in C++?


所以,我想:多态性怎么样?我可以创建一个抽象的

基类命令,只需一个虚函数,然后生成25个/或
从它继承的小类。然后我就可以有一个

泛型execute()函数,如:


void execute_command(Command * c)

{

c-> dosomething();

}


这似乎是执行此操作的标准C ++方式。

尽管如此,有些东西看起来很荒谬。我

定义了25个类,所以我可以动态地在函数之间进行选择。

最重要的是,这是老式的方式(一张普通的表格)
函数指针)效率更高,因为它不涉及

vtable。


出于某种原因,我不是感觉像C ++提供了一个优雅的解决方案来解决这个问题。我只是太挑剔了吗?或者有人同意吗?

评论,想法,建议,将不胜感激。
So, I thought: how about polymorphism? I could create an abstract
base class "command", with a single virtual function, and then make 25
little classes which inherit from it. Then I could just have a
generic execute() function, like:

void execute_command(Command* c)
{
c->dosomething();
}

This seems like it would be the standard C++ way of doing this.
Still, there''s something about it that just seems ridiculous. I
define 25 classes, just so I can pick between functions dynamically.
On top of that, doing this the old fashioned way (a table of plain
function pointers) is more efficient, since it doesn''t involve the
vtable.

For some reason, I don''t feel like C++ offers an elegant solution to
this problem. Am I just being too picky? Or does anyone agree?
Comments, thoughts, suggestions, would be appreciated.



嗯,对于这个简单的东西,

使用类似乎没什么意义,但可能仍然存在这样做有一些好处。


考虑未来的变化。向复杂的条件

语句添加新命令需要一定的努力,即使只是一点点。如果

条件声明(或类似条件)存在于您的程序中的多个

位置,您会发现自己重复同样的努力

。存在这些条件语句的地方。


如果使用多态,则复杂的条件语句(例如

开关)可能会被多态性所取代,而不是只使得代码更容易阅读,而且稍后添加一个新命令可能就像创建一个类来封装所需的行为一样简单。当然,使用适当的方法创建一个

类比编写一个

函数要多得多,但不是很多。通过这样做可以获得的可读性和易于维护的数量可能会超过你花费b / b
的努力。


那个这只是我能想到的一个优势。也许

其他人会提供其他人。


- 丹尼斯

Well, for something this simple, it might not seem to make a lot of sense to
use classes, but there may still be some advantages to doing so.

Consider future changes. Adding a new command to a complex conditional
statement requires a certain amount of effort, even if just a little. If
that conditional statement (or similar conditionals) exists in more than one
place in your program, you will find yourself duplicating that same effort
wherever those conditional statements exist.

If you use polymorphism instead, a complex conditional statement (e.g. a
switch) may be replaced by polymorphism, which not only makes the code
easier to read, but also adding a new command later could be as simple as
creating a class to encapsulate the desired behavior. Granted, creating a
class with the appropriate method is a little more work than writing a
function, but not by much. The amount of readability and ease of
maintenance you gain by doing so will probably outweigh the effort you
expend.

That''s just one advantage I can think of off the top of my head. Perhaps
someone else will offer others.

- Dennis


这篇关于关于多态性的几点思考的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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