ABC没有虚拟功能? [英] ABC w/o virtual functions?

查看:71
本文介绍了ABC没有虚拟功能?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

让我们说我希望有一个抽象基类定义一个方法,它可以实现所有子类实现的方法。


然而,我不能使用虚拟方法(长话短说:这些对象

只有24个字节,我有几百万个。)


我可以定义ABC接口中的方法没有

虚拟化?

正如我所说,所有子类都将提供方法。


谢谢,


Joseph

Let''s say I want to have an abstract base class define a method which
all the subclasses will implement.

However, I cannot use virtual methods (long story short: these objects
are only 24 bytes and I have several million of them).

Is it possible for me to define the method in the ABC interface without
virtuals?
As I said, all the subclasses will provide the method.

Thanks,

Joseph

推荐答案



Joseph Turian写道:

Joseph Turian wrote:
让我们说我想要一个抽象基类定义一个所有子类都将实现的方法。

然而,我不能使用虚拟方法(长话短说:这些对象只有24个字节,我有几百万个。)

我可以在没有
虚拟?
正如我所说的,所有子类都将提供方法。
Let''s say I want to have an abstract base class define a method which
all the subclasses will implement.

However, I cannot use virtual methods (long story short: these objects
are only 24 bytes and I have several million of them).

Is it possible for me to define the method in the ABC interface without
virtuals?
As I said, all the subclasses will provide the method.




抽象函数不能是虚拟的,所以有一次攻击

你。


如果你没有定义虚函数,那么如果你通过父指针调用子类函数将不会调用



如果你能在编译时告诉我们做了什么以及你可以使用谁b / b
使用模板但是如果你想要运行时它也会分解<如果你使用父指针/引用调用,则
会崩溃。


简而言之,提供多态行为的唯一方法是使用

虚函数。您可以将这些对象作为其父对象传递出去,但是这些对象在传递时会表现得像。


我认为你需要看看更密切关注你的问题空间,看看你的解决方案是否更好。



An abstract function cannot be virtual so there is one strike against
you.

If you don''t define the function virtual the subclass functions will
not be called if you are calling through the parent''s pointer.

If you can tell at compile time what is being done and with who you can
use templates but this breaks down if you want runtime and it also
breaks down if you are calling with the parent pointer/reference.

In short, the only way to provide polymorphic behavior is to use
virtual functions. You can pass these objects off as their parent but
that is what they will behave like while being passed off as such.

I think you need to look more closely at your problem space and see if
your solution could be better.


" Joseph Turian" <涂**** @ gmail.com>在消息中写道

news:11 ********************** @ g14g2000cwa.googlegr oups.com ...
"Joseph Turian" <tu****@gmail.com> wrote in message
news:11**********************@g14g2000cwa.googlegr oups.com...
让我们说我想要一个抽象基类定义一个所有子类都将实现的方法。

然而,我不能使用虚方法(长话短说) :这些对象只有24个字节,我有几百万个。)

我可以在没有
虚拟的ABC界面中定义方法吗?正如我所说,所有子类都将提供方法。

谢谢,

约瑟夫
Let''s say I want to have an abstract base class define a method which
all the subclasses will implement.

However, I cannot use virtual methods (long story short: these objects
are only 24 bytes and I have several million of them).

Is it possible for me to define the method in the ABC interface without
virtuals?
As I said, all the subclasses will provide the method.

Thanks,

Joseph




如果该方法是虚拟的还是不应该增加派生类的大小

。继续尝试。方法本身通常不会增加实例大小,但派生类必须大于

基类。换句话说,尝试你的基础和yoru派生的sizeof,使它虚拟成为b $ b,然后再试一次。



If the method is virtual or not should not increase the derived class size
at all. Go ahead and try it. Methods themselves generally do not increase
the size of an instance, although a derived class has to be larger than a
base class. In other words, try sizeof of your base and yoru derived, make
it virtual, then try it again.




roberts.n ... @ gmail.com写道:

roberts.n...@gmail.com wrote:
Joseph Turian写道:
Joseph Turian wrote:
让我们说我想要一个抽象基类定义一个所有子类都将实现的方法。

然而,我不能使用虚方法(长话短说:这些对象只有24个字节,我有几百万个) 。

我可以在没有
虚拟的情况下在ABC界面中定义方法吗?
正如我所说,所有子类都将提供方法。
Let''s say I want to have an abstract base class define a method which
all the subclasses will implement.

However, I cannot use virtual methods (long story short: these objects
are only 24 bytes and I have several million of them).

Is it possible for me to define the method in the ABC interface without
virtuals?
As I said, all the subclasses will provide the method.



抽象函数不能是虚拟的,所以有一次攻击你。



An abstract function cannot be virtual so there is one strike against
you.




我的意思恰恰相反...... ..abstract函数必须是虚拟的
,否则不能。



I meant the exact opposite of course....abstract functions MUST be
virtual and cannot be otherwise.


这篇关于ABC没有虚拟功能?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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