inline virtual functions. | | |
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 | | | | re: inline virtual functions.
"Dave Townsend" <datownsend@comcast.net> wrote...[color=blue]
> 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....![/color]
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 | | | | re: inline virtual functions.
"Dave Townsend" <datownsend@comcast.net> wrote in message
news:LKSdnV2N4cz56DDd4p2dnA@comcast.com[color=blue]
> 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[/color]
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) | | | | re: inline virtual functions.
Dave Townsend wrote:[color=blue]
>
> 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....![/color]
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 | | | | re: inline virtual functions.
"Stephen Waits" <steve@waits.net> wrote in message
news:40AD9901.4070403@waits.net...[color=blue]
>
> Also remember that the inline function-specifier (implicit or explicit)
> is only a recommendation.
>
> See 7.1.2.[/color]
However, inline has a second meaning as a linkage directive!
If you define a function in a header file (not a practice I would
recommend, but for the sake of argument), then only one file
in a multiple source file project may include that header, unless
the definition is tagged as inline. Failing to do that, ambiguities
in name resolution will emerge at link time.
- Risto - | | | | re: inline virtual functions.
"John Carson" <donaldquixote@datafast.net.au> wrote in message news:<40ad8da8$1@usenet.per.paradox.net.au>...[color=blue]
> "Dave Townsend" <datownsend@comcast.net> wrote in message
> news:LKSdnV2N4cz56DDd4p2dnA@comcast.com[color=green]
> > 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[/color]
>
>
> 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.[/color]
Of course, this is not the typical way to call such a function. It smells of
either a questionable design or a performance hack.
There is one exception, though. There is one quite common use of this
mechanism: If the derived class version of a virtual function is supposed
to call the base class version. An example would be a print function where
each derived class adds print statements for its own data. | | | | re: inline virtual functions.
* "John Carson" <donaldquixote@datafast.net.au> schriebt:[color=blue]
> "Dave Townsend" <datownsend@comcast.net> wrote in message
> news:LKSdnV2N4cz56DDd4p2dnA@comcast.com[color=green]
> > 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....![/color]
>
> 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.[/color]
The question was about inline virtual functions, not about inlining of virtual
function calls or of calls of virtual functions.
--
A: Because it messes up the order in which people normally read text.
Q: Why is top-posting such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail? | | | | re: inline virtual functions.
"Alf P. Steinbach" <alfps@start.no> wrote in message
news:40adda84.355493250@news.individual.net[color=blue]
>
> The question was about inline virtual functions, not about inlining
> of virtual function calls or of calls of virtual functions.[/color]
And why do you think that the interviewer bothered to single out inline
virtual functions as opposed to inline functions in general? The only reason
that I can think of is because the rules for inlining are different.
--
John Carson
1. To reply to email address, remove donald
2. Don't reply to email address (post here instead) | | | | re: inline virtual functions.
* "John Carson" <donaldquixote@datafast.net.au> schriebt:[color=blue]
> "Alf P. Steinbach" <alfps@start.no> wrote in message
> news:40adda84.355493250@news.individual.net[color=green]
> >
> > The question was about inline virtual functions, not about inlining
> > of virtual function calls or of calls of virtual functions.[/color]
>
> And why do you think that the interviewer bothered to single out inline
> virtual functions as opposed to inline functions in general? The only reason
> that I can think of is because the rules for inlining are different.[/color]
It was an open question.
An open question can tell much.
--
A: Because it messes up the order in which people normally read text.
Q: Why is top-posting such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail? | | | | re: inline virtual functions.
How about this.
<Foo.h>
Class Foo
{
public:
virtual void func() {}
}
<Bar.h>
#include "Foo.h"
class Bar : public Foo
{
public:
virtual void func();
}
<Bar.cpp>
void Bar::func()
{
}
<Main.cpp>
#include "Foo.h"
#include "Bar.h"
int main()
{
Foo f;
Bar b;
f.func(); // can be resolved at compile time.and is inlined
b.func(); // can be resolved at compile time and is outlined
Foo* fb = new Bar;
fb->func(); // resolved at runtime.
return 1;
} | | | | re: inline virtual functions.
John Carson wrote:[color=blue][color=green]
>> ...
>> 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)
>> ...[/color]
> I think this was a trick question. Virtual function calls are only resolved
> at runtime and cannot be inlined.[/color]
"Inline function" and "inlined function call" are two different and
relatively independent things in C++. The same applies to "virtual
function" and "virtual (dynamically resolved) function call".
The question specifically mentions "inline virtual functions" and make
no reference to function calls whatsoever. Formally, there's absolutely
nothing tricky about it.
--
Best regards,
Andrey Tarasevich | | | | re: inline virtual functions.
Risto Lankinen wrote:[color=blue][color=green]
>>
>> Also remember that the inline function-specifier (implicit or explicit)
>> is only a recommendation.
>>
>> See 7.1.2.[/color]
>
> However, inline has a second meaning as a linkage directive!
>
> If you define a function in a header file (not a practice I would
> recommend, but for the sake of argument), then only one file
> in a multiple source file project may include that header, unless
> the definition is tagged as inline. Failing to do that, ambiguities
> in name resolution will emerge at link time.
> ...[/color]
Exactly. The same can be said about 'virtual'. It also has several
second meanings only loosely related to dynamically resolving function
calls.
When answering such questions, it is very important to understand the
difference between the properties of functions and the properties of
concrete function calls. Unfortunately, in many cases these two got
mixed together, which can only lead to confusion.
--
Best regards,
Andrey Tarasevich | | | | re: inline virtual functions.
"Andrey Tarasevich" <andreytarasevich@hotmail.com> wrote in message
news:10asga07lvggfe0@news.supernews.com[color=blue]
> John Carson wrote:[color=green][color=darkred]
>>> ...
>>> 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)
>>> ...[/color]
>> I think this was a trick question. Virtual function calls are only
>> resolved at runtime and cannot be inlined.[/color]
>
> "Inline function" and "inlined function call" are two different and
> relatively independent things in C++. The same applies to "virtual
> function" and "virtual (dynamically resolved) function call".
>
> The question specifically mentions "inline virtual functions" and make
> no reference to function calls whatsoever. Formally, there's
> absolutely nothing tricky about it.[/color]
We don't have the text of the interview question, just a paraphrase of it.
As I commented in response to Alf, I cannot think why the interviewer would
single out virtual functions for special mention unless (s)he was thinking
of the special inlining rules applicable to them.
--
John Carson
1. To reply to email address, remove donald
2. Don't reply to email address (post here instead) | | | | re: inline virtual functions.
"Alf P. Steinbach" <alfps@start.no> wrote in message
news:40adf4bc.362204812@news.individual.net[color=blue][color=green]
>>
>> And why do you think that the interviewer bothered to single out
>> inline virtual functions as opposed to inline functions in general?
>> The only reason that I can think of is because the rules for
>> inlining are different.[/color]
>
> It was an open question.[/color]
But not open enough to permit a discussion of inlining?
--
John Carson
1. To reply to email address, remove donald
2. Don't reply to email address (post here instead) | | | | re: inline virtual functions.
"Andrey Tarasevich" <andreytarasevich@hotmail.com> wrote in message
news:10asgid64kqg38f@news.supernews.com[color=blue]
> Risto Lankinen wrote:[color=green][color=darkred]
>>>
>>> Also remember that the inline function-specifier (implicit or
>>> explicit) is only a recommendation.
>>>
>>> See 7.1.2.[/color]
>>
>> However, inline has a second meaning as a linkage directive!
>>
>> If you define a function in a header file (not a practice I would
>> recommend, but for the sake of argument), then only one file
>> in a multiple source file project may include that header, unless
>> the definition is tagged as inline. Failing to do that, ambiguities
>> in name resolution will emerge at link time.
>> ...[/color]
>
> Exactly. The same can be said about 'virtual'. It also has several
> second meanings only loosely related to dynamically resolving function
> calls.
>
> When answering such questions, it is very important to understand the
> difference between the properties of functions and the properties of
> concrete function calls.[/color]
I would consider the former to be the union of the latter (and of the syntax
for declaring and defining the functions).
--
John Carson
1. To reply to email address, remove donald
2. Don't reply to email address (post here instead) | | | | re: inline virtual functions.
* "John Carson" <donaldquixote@datafast.net.au> schriebt:[color=blue]
> "Alf P. Steinbach" <alfps@start.no> wrote in message
> news:40adf4bc.362204812@news.individual.net[color=green][color=darkred]
> >>
> >> And why do you think that the interviewer bothered to single out
> >> inline virtual functions as opposed to inline functions in general?
> >> The only reason that I can think of is because the rules for
> >> inlining are different.[/color]
> >
> > It was an open question.[/color]
>
> But not open enough to permit a discussion of inlining?[/color]
Oh, it is.
That too.
An open question can tell much.
--
A: Because it messes up the order in which people normally read text.
Q: Why is top-posting such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail? |  | | | | /bytes/about
We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights.
Get the best answers to your questions from over 226,467 network members.
|