如何在C ++中实现final [英] How do I implement final in C++

查看:82
本文介绍了如何在C ++中实现final的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




我认为我的问题确实是如何在C ++中实现类似java的'

final


长版....


我有一个抽象的基类,由几个具体的继承

类。

我有一组方法,我想在基类中实现

并希望

来强制执行客户端应该将这些消息直接发送到

基类

(而不是通过其派生类)。我猜这类似于Java中的最终

...


如何执行此类政策...


谢谢

Hi,

I think my problem is indeed "how to implement something like java''s
final in C++"

The long version....

I have an abstract base class which is inherited by several concrete
classes.
I have a group of methods that I''d like to implement in the base class
and would like
to mandate that the client should send those messages directly to the
base class
(and not thru its derived classes). I guess that is similar to "final"
in Java...

How do I enforce such policies...

Thanks

推荐答案

Medi Montaseri写道:
Medi Montaseri wrote:
我认为我的问题确实存在如何在C ++中实现类似java'的最终结果

长版本....

我有一个抽象的基础由几个具体的类继承的类。
我有一组方法,我想在基类中实现
并希望
强制要求客户端应该将这些消息直接发送到
基类
(而不是通过其派生类)。我猜这类似于Java中的final


我如何执行此类政策......
I think my problem is indeed "how to implement something like java''s
final in C++"

The long version....

I have an abstract base class which is inherited by several concrete
classes.
I have a group of methods that I''d like to implement in the base class
and would like
to mandate that the client should send those messages directly to the
base class
(and not thru its derived classes). I guess that is similar to "final"
in Java...

How do I enforce such policies...




阅读/有效C ++ /,然后做这三件事:


- 不要在方法上写''虚拟''

- 写一个严厉的措辞评论

- 在同名的heirarchy中不写其他方法


在C ++中,你不会为你不使用的东西买单。不写''虚拟''状态

你不打算在这个

函数的秘密vtable(或其他)中使用一个槽,所以你不会支付它。


你需要最后一项,因为如果你有两个同名的函数,

它们会相互隐藏。指向一种类型的指针,引用或实例

会在编译时将该消息绑定到一个方法,而指针,
引用或另一种类型的实例将绑定到另一种方法。因此,如果你更改指针或引用类型,而不是最终实例,那么你可以默默地改变编译器选择的类型。写下更多

防守,以减少这样的错误的几率:不要给同一个名字的不同东西给予
(除非它们实际上是虚拟覆盖

彼此)。


真实最终的成本用像C ++这样的严格编译的语言将是

太高。


-

Phlip
http://www.c2.com/cgi/wiki?TestFirstUserInterfaces


Medi Montaseri写道:
Medi Montaseri wrote:


我认为我的问题确实是如何实现像java这样的东西最终用C ++

长版本....

我有一个抽象的基类,它由几个具体的继承/>类。
我想在基类中实现一组方法,并希望
强制要求客户端将这些消息直接发送给
基类
(而不是通过它的派生类)。我猜这类似于Java中的final
...

我如何执行此类政策...

谢谢
Hi,

I think my problem is indeed "how to implement something like java''s
final in C++"

The long version....

I have an abstract base class which is inherited by several concrete
classes.
I have a group of methods that I''d like to implement in the base class
and would like
to mandate that the client should send those messages directly to the
base class
(and not thru its derived classes). I guess that is similar to "final"
in Java...

How do I enforce such policies...

Thanks




Youi可以明确地调用基类方法:


class b

{

虚拟空方法(){}

};


class d1:public b

{

void方法();

};


class d2:public d1

{

void method();

};


int main(int argc,char * argv [])

{

d2 var;

var.b :: method(); //调用b''''方法''函数

}


hdh,

marc



Youi can call the baseclass method expicitely:

class b
{
virtual void method() {}
};

class d1: public b
{
void method();
};

class d2: public d1
{
void method();
};

int main( int argc, char* argv[])
{
d2 var;
var.b::method(); // calls b''s ''method'' function
}

hdh,
marc


mo ******* @ netscape.net (Medi Montaseri)在留言新闻中写道:< c9 ************************** @ posting.google。 com> ...
mo*******@netscape.net (Medi Montaseri) wrote in message news:<c9**************************@posting.google. com>...


我认为我的问题确实是如何在C ++中实现类似java'的最终结果 ;

长版本....

我有一个抽象的基类,它由几个具体的类继承。
我有一个组我希望在基类中实现的方法
并且希望
强制要求客户端将这些消息直接发送到
基类
(而不是通过它的派生类)。我猜这类似于Java中的final
...

我如何执行此类政策...

谢谢
Hi,

I think my problem is indeed "how to implement something like java''s
final in C++"

The long version....

I have an abstract base class which is inherited by several concrete
classes.
I have a group of methods that I''d like to implement in the base class
and would like
to mandate that the client should send those messages directly to the
base class
(and not thru its derived classes). I guess that is similar to "final"
in Java...

How do I enforce such policies...

Thanks




添加OP的问题:

_____

| c1 |虚拟空a()= 0;

| _____ | vritual void b()= 0;

^

___ | ___

__ | __ __ | __

实施b | c2 | | c3 |两者都实现了一个

虚拟空虚b(); | _____ | | _____ |虚拟空a();

^

___ | ____

__ | __ __ | __

| c4 | | c5 |不应该重新实现

| _____ | | _____ |两个实现b


如果我有上面显示的情况,有没有办法阻止从c3派生的类的实现者(c4和c5)图)

意外重新实现方法a?从某种意义上说,我的意思是一个编译器错误

就像我想在Java中显示的那个人试图重新实现

a方法被宣布为final。

谢谢


Marcelo Pinto



Adding to the OP''s question:
_____
| c1 | virtual void a() = 0;
|_____| vritual void b() = 0;
^
___|___
__|__ __|__
implements b | c2 | | c3 | both implements a
virtual void b();|_____| |_____| virtual void a();
^
___|____
__|__ __|__
| c4 | | c5 | NOT supposed to REimplement a
|_____| |_____| both implement b

If I have the situation shown above, is there a way to prevent the
implementers of classes derived from c3 (c4 and c5 in the diagram) to
accidentally reimplement method a? By a way I mean a compiler error
like the one I suppose is shown in Java when one tries to reimplement
a method declared as final.

Thanks

Marcelo Pinto


这篇关于如何在C ++中实现final的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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