虚拟方法? [英] virtual methods ?

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

问题描述

我已经看过一些c ++代码,其中一个方法之前有一个很棒的例子:

class foo {

public:

virtual bool方法();

}


虚拟做什么?

解决方案

Geiregat Jonas写道:

我看过一些c ++代码,其中一个方法之前有一个很棒的例子:
class foo {
public:
虚拟bool方法();
}
什么是虚拟的?




阅读你的C ++教科书或参考手册。如果它没有b $ b解释虚函数,那就更好一点。


参见FAQ:
http://www.parashift.com/c ++ - faq-lit ... functions.html


请务必首先研究参考手册,然后再查看常见问题解答

然后搜索新闻组。


-

Thomas Matthews


C ++新闻组欢迎辞:
http://www.slack.net/~shiva/welcome.txt

C ++ Faq : http://www.parashift.com/c++-faq-lite

C常见问题: http://www.eskimo.com/~scs/c-faq/top.html

alt.comp.lang.learn.c-c ++ faq:
< a rel =nofollowhref =http://www.raos.d emon.uk/acllc-c++/faq.html\"target =_ blank> http://www.raos.demon.uk/acllc-c++/faq.html

其他网站:
http://www.josuttis.com - C ++ STL Library书


Geiregat Jonas写道:

我看过一些C ++代码之前的方法vitual ex:

class foo {
公共:
虚拟bool方法(无效);
}

虚拟做什么?




这意味着调用bool方法(void)

实际上可以解析为函数定义

for来自foo的类。


David Cattarin写道:

Adie< a _ ***** *****@hotmail.com>在消息新闻中写道:< 7f ******************************** @ 4ax.com>。 ..

Geiregat Jonas写道:

>我已经看过一些c ++代码,其中一个方法之前有一个虚拟的ex:
> ;类foo {
> public:
>虚拟布尔方法();
>}
>
>虚拟做什么?



添加一点点香料。


[snip]

Base * someObject;
someObject = new DerivedB; //它是一个DerivedB对象
someObject-> aVirtual(x); //做其他事情

关键在于通过使方法成为虚拟的虚拟对象来实现。编译器允许在运行时访问每个派生类函数,否则你将会被Base's卡住。



那不太对。如果没有虚拟,编译器会根据指针类型选择
方法。

Base * bObj;
Derived * dObj = new Derived;
bObj = dObj

bObj-> fn(); //调用B'的fn版本()
dObj-> fn(); //调用D的fn版本()




对不起,以为这就是我的目标。


这可能是一个更好的例子吗?


基数*基数;

基数=新基数;

base-> f (); //调用f()的基本版本

删除基数;

base = new DerivedA;

base-> f(); //调用DerivedA版本的f()

删除基数;

base = NULL;


我没试过这个,我我猜它应该没问题。


I''ve seen some c++ code where before a method there was vitual ex:
class foo{
public:
virtual bool method();
}

what does virtual do ?

解决方案

Geiregat Jonas wrote:

I''ve seen some c++ code where before a method there was vitual ex:
class foo{
public:
virtual bool method();
}

what does virtual do ?



Read your C++ text book or reference manual. If it doesn''t
explain virtual functions, then get a better one.

See the FAQ:
http://www.parashift.com/c++-faq-lit...functions.html

Always research the reference manual first, the FAQ second
then search the newsgroups.

--
Thomas Matthews

C++ newsgroup welcome message:
http://www.slack.net/~shiva/welcome.txt
C++ Faq: http://www.parashift.com/c++-faq-lite
C Faq: http://www.eskimo.com/~scs/c-faq/top.html
alt.comp.lang.learn.c-c++ faq:
http://www.raos.demon.uk/acllc-c++/faq.html
Other sites:
http://www.josuttis.com -- C++ STL Library book


Geiregat Jonas wrote:

I''ve seen some C++ code where before a method there was vitual ex:

class foo{
public:
virtual bool method(void);
}

what does virtual do?



It means that an invocation of bool method(void)
may actually resolve to the function definition
for a class derived from foo.


David Cattarin wrote:

Adie <a_**********@hotmail.com> wrote in message news:<7f********************************@4ax.com>. ..

Geiregat Jonas wrote:

>I''ve seen some c++ code where before a method there was vitual ex:
>class foo{
>public:
> virtual bool method();
>}
>
>what does virtual do ?



Adds a little spice.


[snip]

Base* someObject;
someObject = new DerivedB; // it''s a DerivedB obj
someObject->aVirtual(x);// does something else

The point is that by making method aVirtual "virtual" the compiler allows
access to each derived classes functions at runtime, otherwise you will be
stuck with Base''s.



That''s not quite right. Without virtual, the compiler chooses the
method based on the pointer type.

Base* bObj;
Derived* dObj = new Derived;
bObj = dObj

bObj->fn( ); // Calls B''s version of fn( )
dObj->fn( ); // Calls D''s version of fn( )



Sorry, thought that was what I was getting at.

Might this be a better example?

Base* base;
base = new Base;
base->f(); // calls base version of f()
delete base;
base = new DerivedA;
base->f(); // calls DerivedA version of f()
delete base;
base = NULL;

I havent tried this, I''m guessing that it should be ok.


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

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