纯成员函数 [英] pure member functions

查看:53
本文介绍了纯成员函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




我试图创建一个抽象类,让非虚拟成员

函数为纯,但是我得到了编译错误说只有虚拟的

成员函数可以是纯粹的...


我正在考虑这个限制背后的原因...我只是希望

想要一个基类是抽象的,以避免从派生类型切换到基类型的对象切换到基础类型,在我的情况下基类没有

需要有一个虚拟功能。


还有其他选择吗?只有虚拟

成员函数纯粹的原因是什么?

Hi,

I tried to create a abstract class by having a non-virtual member
function as pure, but i got a compilation error saying "only virtual
member functions can be pure"...

I''m trying to think the reason behind this restriction... i just want
to want a base class to be abstract so as to avoid object slicing into
the base type from derived type, and in my case base class doesn''t
need to have a virtual function.

Is there any alternative? and what is the reason behind only virtual
member functions being pure?

推荐答案

12月18日晚上11:52 ,Rahul< sam _... @ yahoo.co.inwrote:
On Dec 18, 11:52 pm, Rahul <sam_...@yahoo.co.inwrote:




我试图创建一个通过拥有一个非虚拟成员的抽象类

作为纯函数,但我得到一个编译错误说只有虚拟

成员函数可以是纯粹的...... br />

我正在考虑这个限制背后的原因...我只想要

想要一个基类是抽象的,以避免对象从派生类型切入

基类型,在我的情况下基类不需要
需要有虚函数。


还有其他选择吗?只有虚拟

成员函数纯粹背后的原因是什么?
Hi,

I tried to create a abstract class by having a non-virtual member
function as pure, but i got a compilation error saying "only virtual
member functions can be pure"...

I''m trying to think the reason behind this restriction... i just want
to want a base class to be abstract so as to avoid object slicing into
the base type from derived type, and in my case base class doesn''t
need to have a virtual function.

Is there any alternative? and what is the reason behind only virtual
member functions being pure?



如果你没有任何虚函数,可能至少你会得到一个虚拟析构函数。
虚拟析构函数。使它纯净,提供实现。如果一个

非虚拟函数是纯粹的 - 你会如何覆盖它?如果你

不会覆盖它,那是什么意思?没有。所以,没有必要使用这样的会员。

If you don''t have any virtual function, probably atleast you will have
a virtual destructor. Make it pure, provide an implementation. IF a
non-virtual function is pure - how would you override it? And if you
will not override it, what does it mean? Nothing. So, no use having
such members.


2007-12-18 13:52:49 -0500,Rahul< sa * ****@yahoo.co.insaid:
On 2007-12-18 13:52:49 -0500, Rahul <sa*****@yahoo.co.insaid:




我试图创建一个抽象类一个非虚拟成员

作为纯函数,但是我得到了一个编译错误,说只有虚拟

成员函数可以是纯粹的......


我正在考虑这个限制背后的原因...我只想要

想要一个基类是抽象的,以避免对象切入

来自派生类型的基类型,在我的情况下基类没有
需要有一个虚函数。
Hi,

I tried to create a abstract class by having a non-virtual member
function as pure, but i got a compilation error saying "only virtual
member functions can be pure"...

I''m trying to think the reason behind this restriction... i just want
to want a base class to be abstract so as to avoid object slicing into
the base type from derived type, and in my case base class doesn''t
need to have a virtual function.



我不明白你的目标。你是什​​么意思对象切换成

派生类型的基本类型?

I don''t understand your goal. What do you mean by "object slicing into
the base type from derived type?"


>

还有其他选择吗?只有虚拟

成员函数纯粹背后的原因是什么?
>
Is there any alternative? and what is the reason behind only virtual
member functions being pure?



-


-kira

--

-kira


12月18日晚上11点56分,Abhishek Padmanabh< abhishek.padman ... @ gmail.com>

写道:
On Dec 18, 11:56 pm, Abhishek Padmanabh <abhishek.padman...@gmail.com>
wrote:

12月18日晚上11点52分,Rahul< sam _... @ yahoo.co.inwrote:
On Dec 18, 11:52 pm, Rahul <sam_...@yahoo.co.inwrote:


Hi,


我尝试通过将非虚拟成员

函数作为纯函数来创建抽象类,但是我得到了一个编译错误,说只有虚拟

成员函数可以是纯粹的... ...
I tried to create a abstract class by having a non-virtual member
function as pure, but i got a compilation error saying "only virtual
member functions can be pure"...


我正在考虑这个限制背后的原因..我只想要

想要一个基类是抽象的,以避免对象切换到派生类型的基本类型

,在我的情况下基类没有'$

需要有虚拟功能。
I''m trying to think the reason behind this restriction... i just want
to want a base class to be abstract so as to avoid object slicing into
the base type from derived type, and in my case base class doesn''t
need to have a virtual function.


还有其他选择吗?只有虚拟

成员函数纯粹背后的原因是什么?
Is there any alternative? and what is the reason behind only virtual
member functions being pure?



如果你没有虚函数,可能至少你会得到一个虚拟析构函数。使它纯净,提供实现。如果一个

非虚拟函数是纯粹的 - 你会如何覆盖它?如果你

不会覆盖它,那是什么意思?没有。所以,没有必要使用这样的会员。


If you don''t have any virtual function, probably atleast you will have
a virtual destructor. Make it pure, provide an implementation. IF a
non-virtual function is pure - how would you override it? And if you
will not override it, what does it mean? Nothing. So, no use having
such members.



是的,你可能是指当前的标准和

c ++的实现,然而,当bb标准正在制定中,有些人可能会想到纯粹的普通会员功能和一种机制来覆盖他们......因为某些原因他们还没有完成那......这是我想知道的......

Yes, you are probably referring to current standards and
implementation of c++,
however, when the standard was being developed, some one could have
thought of pure ordinary member functions and a mechanism to override
them too... for some reason they haven''t done that... which is what i
was wondering about...


这篇关于纯成员函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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