内联虚函数。 [英] inline virtual functions.

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

问题描述

哟,


我今天接受了面试,面试时问我关于

内联虚拟功能,或者我对他们的看法。

嗯,我在参考资料中看到过这些婴儿,

但是我从未使用过。 (我是一位经验丰富的软件开发人员

并使用C ++超过10年)


任何人都发现必须使用其中一个人或者很有用。

好​​奇的头脑需要知道下一个曲线球问题来了我的

方式....!


谢谢,dave

Yo,

I had a job interview today, the interviewing asked me about
inline virtual functions, or what was my opinion on them.
Hm, I''ve seen mention of these babies in the reference material,
but I''ve never used one. ( I''m an experienced software developer
and have used C++ for more than 10 years)

Anybody found the use of one of these guys necessary or useful.
Curious minds need to know for the next curve ball question coming my
way....!

thanks, dave

推荐答案

" Dave Townsend" <哒******** @ comcast.net>写了...
"Dave Townsend" <da********@comcast.net> wrote...
今天我接受了面试,面试时问了我关于内联虚拟功能,或者我对他们有什么看法。
嗯,我已经在参考资料中看到这些婴儿的提及,
但我从未使用过。 (我是一位经验丰富的软件开发人员,并且已经使用C ++超过10年了)

任何人都发现使用其中一个必要或有用的人。
好奇的头脑需要知道下一个曲线球问题来了我的方式....!
I had a job interview today, the interviewing asked me about
inline virtual functions, or what was my opinion on them.
Hm, I''ve seen mention of these babies in the reference material,
but I''ve never used one. ( I''m an experienced software developer
and have used C++ for more than 10 years)

Anybody found the use of one of these guys necessary or useful.
Curious minds need to know for the next curve ball question coming my
way....!




我不确定我理解你与那些困惑的原因

功能。首先,''inline''不是一个函数的真正属性,而只是一个建议。其次,类定义中定义的任何函数都是隐式内联。第三,一个函数的虚拟性和它的''inline-ness''是两个完全正交的质量,没有什么特别的关于它们共存

就像''const''和''virtual''或''const''和''inline''。


所以,我打赌在你的10年里你有写得像


class Base {

public:

virtual~Base(){} //什么都不做 - 没有数据

虚拟void someFoo()= 0;

虚拟void someBar()const = 0;

};


突然你将''Base'的析构函数定义为(喘气!)

内联和虚拟。啊,那就是内联虚拟功能看起来像

...


来吧,给自己一个休息时间。面试(如果你不定期在
a上进行)很有压力,有些人甚至可以忘记

乘法表,更不用说内联虚拟函数了。 />

Victor



I am not sure I understand the reason for your puzzlement with those
functions. First of all, ''inline'' is not a real attribute of
a function, but a mere suggestion. Second, any function that is
defined in the class definition is implicitly ''inline''. Third,
a virtuality of a function and its ''inline-ness'' are two completely
orthogonal qualities, there is nothing special about them coexisting
just like ''const'' and ''virtual'' or ''const'' and ''inline''.

So, I bet during your 10 years you have written something like

class Base {
public:
virtual ~Base() {} // does nothing -- no data
virtual void someFoo() = 0;
virtual void someBar() const = 0;
};

and suddenly you have the destructor for ''Base'' defined as (gasp!)
inline and virtual. Ah, that''s what "inline virtual" functions look
like! And I''ve been using those all the time without even knowing
that...

Come on, give yourself a break. Interviews (if you don''t do them on
a regular basis) are stressful and some people can even forget the
multiplication table, let alone inline virtual functions.

Victor


" Dave Townsend" <哒******** @ comcast.net>在消息中写道

新闻:LK ******************** @ comcast.com
"Dave Townsend" <da********@comcast.net> wrote in message
news:LK********************@comcast.com
哟,

我今天接受了面试,面试时问了我关于内联虚拟功能,或者我对他们有什么看法。
嗯,我看到过这些婴儿的提及参考资料,
但我从来没用过。 (我是一位经验丰富的软件开发人员,并且已经使用C ++超过10年了)

任何人都发现使用其中一个必要或有用的人。
好奇的头脑我需要知道接下来的曲线球问题我的方式....!

谢谢,戴夫
Yo,

I had a job interview today, the interviewing asked me about
inline virtual functions, or what was my opinion on them.
Hm, I''ve seen mention of these babies in the reference material,
but I''ve never used one. ( I''m an experienced software developer
and have used C++ for more than 10 years)

Anybody found the use of one of these guys necessary or useful.
Curious minds need to know for the next curve ball question coming my
way....!

thanks, dave



我认为这是一招题。虚函数调用仅在运行时解析为
且无法内联。如果直接调用虚函数(即,使用类对象

而不是指针或引用,或者使用范围分辨率<),则对此语句的限定为

br />
运算符完全指定相关函数),然后它是非虚拟的

用于该调用的目的并且可以内联。

- -

John Carson

1.要回复电子邮件地址,请删除donald

2.不要回复电子邮件地址(在这里发布)相反)


I think this was a trick question. Virtual function calls are only resolved
at runtime and cannot be inlined. The qualification to this statement is
that if a virtual function is called directly (i.e., using a class object
rather than a pointer or reference or else using the scope resolution
operator to fully specify the function concerned), then it is non-virtual
for the purposes of that call and can be inlined.
--
John Carson
1. To reply to email address, remove donald
2. Don''t reply to email address (post here instead)


Dave Townsend写道:
Dave Townsend wrote:

我今天接受了面试,面试时问我<内联虚拟功能,或者我对它们的看法。
嗯,我在参考资料中看到过这些婴儿的提及,
但我从未使用过。 (我是一位经验丰富的软件开发人员,并且已经使用C ++超过10年了)

任何人都发现使用其中一个必要或有用的人。
好奇的头脑需要知道下一个曲线球问题来了我的方式....!

I had a job interview today, the interviewing asked me about
inline virtual functions, or what was my opinion on them.
Hm, I''ve seen mention of these babies in the reference material,
but I''ve never used one. ( I''m an experienced software developer
and have used C++ for more than 10 years)

Anybody found the use of one of these guys necessary or useful.
Curious minds need to know for the next curve ball question coming my
way....!




编译器不能[总是]知道哪个虚拟将被称为

因此它无法内联函数,因为它[通常]必须在运行时[通过vtable查找]绑定



struct Base

{

virtual void Blah(){; }

};


struct Sub:Base

{

/ * virtual * / void Blah(){; }

};


void SomeFunc(Base * obj)

{

obj-> ;布拉赫(); //无法知道在这里内联哪个函数!

}

当然有一些情况下编译器可以确定

需要哪个函数被称为,因此可能会内联它。我不知道

某些热门编译器在这种情况下做了什么。一旦这样的情况:


void OtherFunc()

{

Sub s;

s。等等(); //总是无法使用

}


所以,虽然它不应该伤害任何东西,但它通常也不会得到

内联。


还要记住内联函数说明符(隐式或显式)

只是推荐。

见7.1.2。


- 智慧



The compiler can''t [always] know for certain which virtual will be
called.. therefore it cannot inline the function as it [often] must be
bound at runtime [via vtable lookup].

struct Base
{
virtual void Blah() { ; }
};

struct Sub: Base
{
/*virtual*/ void Blah() { ; }
};

void SomeFunc( Base* obj )
{
obj->Blah(); // impossible to know which function to inline here!
}
There are of course cases when the compiler can determine which function
needs to be called, and might therefore inline it. I don''t know for
certain what the popular compilers do in this case. Once such case:

void OtherFunc()
{
Sub s;
s.Blah(); // always inlinable
}

So, while it shouldn''t hurt anything, it probably also won''t usually get
inlined.

Also remember that the inline function-specifier (implicit or explicit)
is only a recommendation.

See 7.1.2.

--Steve


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

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