虚拟关键字 [英] the virtual keyword

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

问题描述

我的理解是,如果你要将一个函数声明为虚拟

,你就无法在类中声明它被声明为虚拟的物体,

但是今晚玩弄它,它让我这样做。以及

以下代码


class a

{

public:virtual void fun( ){std :: cout<< a类 <<的std :: ENDL; };

};


b级:公共类a

{

public:void fun(){std :: cout<< b类 <<的std :: ENDL; };

};


int main()

{

a aa;

b bb;

a * p;

p =& aa;

p-> fun();

p =& bb;

p-> fun();

返回0;

}


它打印出来

class a

class b


我的问题是什么是好处对此,这是什么降价?

这个?我有一个使用这种能力,但有些东西告诉我有一些我想要忽视的东西,我可能会在

路上遇到问题。我是偏执狂吗?

it was my understanding that if you were to declare a function as virtual
you could not declare a body for it in the class it was declared virtual,
but playing around with it tonight, it let me do this. and with the
following code

class a
{
public: virtual void fun() {std::cout << "class a" << std::endl; };
};

class b : public class a
{
public: void fun() { std::cout << "class b" << std::endl; };
};

int main()
{
a aa;
b bb;
a *p;
p = &aa;
p->fun();
p = &bb;
p->fun();
return 0;
}

it printed out
class a
class b

my question is what is the benefits to this, what are the down falls to
this? I have a use for this ability but something tells me there is
something that I am overlooking and I might run into problems down the
road. So am I being paranoid?

推荐答案




如果你不使用虚拟


然后甚至你覆盖父母的方法,当你


a obj = new b();

a-> fun();


它会在课堂上调用有趣的方法而不是课程b


但是,如果你使用虚拟,它会在课堂上调整方法b


i我不确定它的好处,但我知道有一些不愉快

此功能的结果: )


所以,记得在需要多态时使用虚拟


-

最好的问候!

Sowen Cheung
http://com.angGoGo.com
http://www.angGoGo.com
http://biz.angGoGo.com

" Michael P. O康纳" <熔点** @ mikeoconnor.net>在留言中写道

news:pa **************************** @ mikeoconnor.ne t ...


if you don''t use virtual

then even u override the parent''s method, when u

a obj = new b();
a->fun();

it will call the fun method in class a not class b

but, if u use virtual, it will cal the method in class b

i am not sure the benefits of it, but i know there are some unpleasure
results of this feature :)

so, remember to use virtual when u need polymorphism

--
Best Regards!
Sowen Cheung
http://com.angGoGo.com
http://www.angGoGo.com
http://biz.angGoGo.com
"Michael P. O''Connor" <mp**@mikeoconnor.net> wrote in message
news:pa****************************@mikeoconnor.ne t...
我的理解是,如果你要将一个函数声明为虚拟
你不能在类中声明它的身体它被宣布为虚拟,
但是玩弄它今晚,它让我这样做。以及
以下代码

类a
公共:虚拟空闲fun(){std :: cout<< a类 <<的std :: ENDL; };
};

b类:公共类a
公共:void fun(){std :: cout<< b类 <<的std :: ENDL; };
};

int main()
{
a aa;
b bb;
a * p;
p =& aa;
p-> fun();
p =& bb;
p-> fun();
返回0; 一堂课


我的问题是这有什么好处,有什么下降到这个?我对这种能力有用,但有些东西告诉我有些东西是我忽略的,我可能会遇到问题。那我是偏执狂吗?
it was my understanding that if you were to declare a function as virtual
you could not declare a body for it in the class it was declared virtual,
but playing around with it tonight, it let me do this. and with the
following code

class a
{
public: virtual void fun() {std::cout << "class a" << std::endl; };
};

class b : public class a
{
public: void fun() { std::cout << "class b" << std::endl; };
};

int main()
{
a aa;
b bb;
a *p;
p = &aa;
p->fun();
p = &bb;
p->fun();
return 0;
}

it printed out
class a
class b

my question is what is the benefits to this, what are the down falls to
this? I have a use for this ability but something tells me there is
something that I am overlooking and I might run into problems down the
road. So am I being paranoid?



我确实看到当我运行测试程序时,我有一个程序我是

在这种情况下需要多态性,我只想知道

的垮台可能是什么,我应该看看其他选项,如果出现问题

重量好处,所以我只需要知道问题,所以我可以在继续前进之前权衡


I did see that when I ran the test program, and I have in a program I am
working a need for polymorphism in this case, I just want to know what the
downfalls might be, should I look at other options, if the problems out
weigh the benefits, so I just need to know the problems, so I can weigh
the options before I continue to far on.


Michael P O''Connor写道:
Michael P. O''Connor wrote:
我的理解是,如果你要将一个函数声明为虚拟
你不能在类中声明它的主体,它被声明为虚拟,
it was my understanding that if you were to declare a function as virtual
you could not declare a body for it in the class it was declared virtual,




这是不正确的。如果在类定义中定义成员函数虚拟甚至

,也可以。但是,在类

定义中定义的函数发生的隐式

内联提示在这种情况下可能实际上不会导致内联。


由于虚拟函数可能被覆盖,因此必须在运行时确定在程序中的特定点调用的实际函数

(通常通过查找它在一个表中的地址由一个

实现生成的对象中的隐藏字段指向。只有当

编译器可以确定在编译时调用哪个函数时,通常有可能使用b $ b内联。例如,


obj-> func();


将导致查询要调用的函数的地址,所以

它不能被内联。另一方面,


obj-> Obj :: func();


可能内联,因为编译器知道特定的功能你是

试图打电话。


Gregg



That is not correct. It is ok to declare a member function virtual even
if you define it within the class definition. However, the implicit
inlining hint that occurs with functions defined within the class
definition will probably not actually result in inlining in this case.

Since virtual functions may be overridden, the actual function that gets
called at a specific point in your program must be determined at runtime
(typically by looking up its address in a table pointed to by a an
implementation-generated hidden field in the object). Only if the
compiler can determine which function to call at compile-time is
inlining usually a possibility. For example,

obj->func();

will result in a table lookup of the address of the function to call, so
it can''t be inlined. On the other hand,

obj->Obj::func();

may be inlined since the compiler knows the specific function you are
trying to call.

Gregg


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

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