如何使多态可选? [英] How make polymorphism optional?

查看:71
本文介绍了如何使多态可选?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的问题是以下一个。


我有大量的基类对象:

class Base {

public:

虚拟无效

方法();

}


及派生class:

class派生:publcic Base {

public:

virtual void

method();

}


有时我没有派生类的对象,在这种情况下

我想摆脱多态性开销。 (速度至关重要

for

me)。我可以使用单独的二进制文件来处理这些情况。但是我提出的唯一一个

的设计是

,预处理器是分开的类定义中的虚拟关键字


class Base {

#ifdefine NOPOLYMORPHISM

void

method( );

#else

虚拟无效

方法();

#endif

}


和程序中具体类型的对象是

定义的部分应该

也可以修改。


有没有更好的方法呢?

My problem is the following one.

I have a huge number of objects of base class:
class Base {
public:
virtual void
method();
}

And a derived class:
class Derived : publcic Base {
public:
virtual void
method();
}

Sometime I have no objects of Derived class and in those cases
I would like to get rid of polymorphism overhead. (speed is crucial
for
me). It is OK for me to have
a separate binary to handle those cases. But the only design I came up
with is
with preprocessor to "separate" virtual keyword in class definition

class Base {
#ifdefine NOPOLYMORPHISM
void
method();
#else
virtual void
method();
#endif
}

and the part of the program where the concrete type of the objects is
defined should
be also modified.

Is there any better way to do that?

推荐答案

On 2008-09-06 17 :09,Litvinov Sergey写道:
On 2008-09-06 17:09, Litvinov Sergey wrote:

我的问题是以下一个。


我有大量的对象基础类:

类基础{

公共:

虚拟空白

方法();

}


派生类:

class派生:publcic Base {

public:

虚拟无效

方法();

}


有时我没有De的对象rived class和那些情况

我想摆脱多态性开销。 (速度至关重要

for

me)。
My problem is the following one.

I have a huge number of objects of base class:
class Base {
public:
virtual void
method();
}

And a derived class:
class Derived : publcic Base {
public:
virtual void
method();
}

Sometime I have no objects of Derived class and in those cases
I would like to get rid of polymorphism overhead. (speed is crucial
for
me).



我不得不问,你有没有测量并确定它是你的性能问题的

多态?如果你还没有仔细地说明你的程序,你应该这样做,然后再考虑如何加速你的b $ b。


如果你没有使用Derived类,我认为最简单的方法是

只需#ifdef声明它,编译器应该是

能够优化掉多态性。当然,还有一个

的机会,它已经可以随时使用。


-

Erik Wikstr? ?m

I have to ask, have you measured and made sure that it is the
polymorphism that is your performance problem? If you have not carefully
profiled your program yet you should do so before you consider how to
speed it up.

If you do not use the Derived class I think the easiest way would be to
simply #ifdef out the declaration of it, the compiler should then be
able to optimise away the polymorphism. Of course, there is also a
chance that it already does so whenever it can.

--
Erik Wikstr??m


Litvinov Sergey< sl ******* @ gmail.comwrote:
Litvinov Sergey <sl*******@gmail.comwrote:

我的问题是以下一个。


我有大量的基类对象:

class Base {

public :

虚拟无效

方法();

}


和派生类:

类派生:publcic Base {

public:

virtual void

method();

}


有时我没有Derived类的对象,在这种情况下我想要摆脱多态性开销。 (速度至关重要

对我来说)。
My problem is the following one.

I have a huge number of objects of base class:
class Base {
public:
virtual void
method();
}

And a derived class:
class Derived : publcic Base {
public:
virtual void
method();
}

Sometime I have no objects of Derived class and in those cases
I would like to get rid of polymorphism overhead. (speed is crucial
for me).



如果速度至关重要,你应该剖析并找出最佳花费的地方

你的努力,这不太可能是其中之一在任何情况下:


vector< Basevec(1); //使向量大到

//你有多少个对象

vec [0] .method(); //没有虚拟开销。

If speed is crucial, you should profile and find out where best to spend
your efforts, this is unlikely to be one of those places.

In any case:

vector<Basevec(1); // make the vector as big as the
// number of objects you have
vec[0].method(); // no virtual overhead.


Daniel T.写道:
Daniel T. wrote:

Litvinov Sergey< sl *** **** @ gmail.com写道:
Litvinov Sergey <sl*******@gmail.comwrote:

>我的问题是以下一个。

我有大量的对象基类:
类基础{
公共:
虚拟空白
方法();
}

派生类:
class派生:publcic Base {
public:
virtual void
method();
}

有时我没有Derived类的对象和在那些情况下
我想摆脱多态性开销。 (速度对我来说至关重要)。
>My problem is the following one.

I have a huge number of objects of base class:
class Base {
public:
virtual void
method();
}

And a derived class:
class Derived : publcic Base {
public:
virtual void
method();
}

Sometime I have no objects of Derived class and in those cases
I would like to get rid of polymorphism overhead. (speed is crucial
for me).



如果速度至关重要,你应该剖析并找出最好的地方花费你的努力,这不太可能是其中之一在任何情况下:


vector< Basevec(1); //使向量大到

//你有多少个对象

vec [0] .method(); //没有虚拟开销


If speed is crucial, you should profile and find out where best to
spend your efforts, this is unlikely to be one of those places.

In any case:

vector<Basevec(1); // make the vector as big as the
// number of objects you have
vec[0].method(); // no virtual overhead.



你确定没有虚拟开销吗? vector :: operator []

返回* reference *,而不是对象。通过引用调用

虚拟函数*应该*通过解析所有

的机制,虚函数调用最终的覆盖。它不是

静态地解析为Base ::方法,如果这就是你所暗示的。


V

-

请在通过电子邮件回复时删除资金''A'

我没有回复最热门的回复,请不要问

Are you sure about "no virtual overhead"? The vector::operator[]
returns a *reference*, not an object. With a reference a call to
a virtual function *should* go through the mechanism resolving all
the virtual function calls to the final overrider. It''s not
statically resolved to Base::method, if that''s what you implied.

V
--
Please remove capital ''A''s when replying by e-mail
I do not respond to top-posted replies, please don''t ask


这篇关于如何使多态可选?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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