virtual inline

R

.rhavin grobert

if you have

class Base {
virtual inline bool foo() {return false;};
};

class Derived: public Base {
virtual bool foo();
};


bool Derived:foo()
{
/* lot's of code here */
return true;
};

is foo() always inlined for Base? is foo always (jumped /
called / ...) (<-?) for Derived?
 
M

Marcel Müller

Hi!

..rhavin grobert said:
class Base {
virtual inline bool foo() {return false;};
};

class Derived: public Base {
virtual bool foo();
};

bool Derived:foo()
{
/* lot's of code here */
return true;
};

is foo() always inlined for Base? is foo always (jumped /
called / ...) (<-?) for Derived?

From your code Base::foo is never called at all, so you cannot answer
the question whether it is expanded inline or not.

Derived::foo cannot be expanded inline unless your compiler uses a two
pass method to examine the body of the function.

Furthermore, a virtual function may not be expanded inline unless the
compiler knows for sure, that it cannot be overloaded. The reason is
simply that the required function is determined at runtime and may not
even be written at the time when the compiler generates the call.
Only if the type is known for sure, the run time dispatch can be
optimized. E.g.
Base b;
b.foo();
may expand Base::foo inline.
But as soon as b becomes a reference or pointer type, a run time
dispatch is required.
(Languages with a 'final' attribute for functions and classes have a
significantly higher probability of the above optimization.)


Marcel
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,997
Messages
2,570,240
Members
46,830
Latest member
HeleneMull

Latest Threads

Top