473,413 Members | 1,733 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,413 software developers and data experts.

inline virtual functions

Dear experts,

A virtual function has to have an address. So if an inline virtual
function is actually inlined then in that case what does address of
this function signify? How does compiler know at compile time about
the actual object a pointer points to so that it can paste the correct
inline function in the place of function call if the function is
getting inlined?

Jul 3 '07 #1
8 2976
siddhu wrote:
Dear experts,

A virtual function has to have an address.
Really? Why?
So if an inline virtual
function is actually inlined then in that case what does address of
this function signify?
If a function is inlined, the compiler is still free to create a body
of it _if_ it needs to take the address.
How does compiler know at compile time about
the actual object a pointer points to so that it can paste the correct
inline function in the place of function call if the function is
getting inlined?
It doesn't. The whole point of polymorphism is that the type of the
actual object is unknown until the run time.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Jul 3 '07 #2
On Jul 3, 10:56 am, "Victor Bazarov" <v.Abaza...@comAcast.netwrote:
siddhu wrote:
Dear experts,
A virtual function has to have an address.

Really? Why?
So if an inline virtual
function is actually inlined then in that case what does address of
this function signify?

If a function is inlined, the compiler is still free to create a body
of it _if_ it needs to take the address.
How does compiler know at compile time about
the actual object a pointer points to so that it can paste the correct
inline function in the place of function call if the function is
getting inlined?

It doesn't. The whole point of polymorphism is that the type of the
actual object is unknown until the run time.
Without knowing the exact type how can a compiler expand the function
at function call?
>
V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask

Jul 3 '07 #3
siddhu wrote:
>>>How does compiler know at compile time about
the actual object a pointer points to so that it can paste the correct
inline function in the place of function call if the function is
getting inlined?
On Jul 3, 10:56 am, "Victor Bazarov" <v.Abaza...@comAcast.netwrote:
>>It doesn't. The whole point of polymorphism is that the type of the
actual object is unknown until the run time.
siddhu wrote:
Without knowing the exact type how can a compiler expand the function
at function call?
If the compiler doesn't know which function will be used, it cannot expand it.
As the inline keyword doesn't _require_ the compiler to expand functions (the
inline keyword is only a _suggestion_ that the method should be expanded), the
compiler is free to simply call the function.

BTW, inline and virtual don't go together. As I recall, compilers may even issue
a warning if you try to inline virtual methods.

Regards,
Stuart
Jul 3 '07 #4
On Jul 3, 10:44 am, siddhu <siddharth....@gmail.comwrote:
Dear experts,

A virtual function has to have an address. So if an inline virtual
function is actually inlined then in that case what does address of
this function signify? How does compiler know at compile time about
the actual object a pointer points to so that it can paste the correct
inline function in the place of function call if the function is
getting inlined?
http://www.devx.com/tips/Tip/13815

Jul 4 '07 #5
On Jul 3, 4:56 pm, "Victor Bazarov" <v.Abaza...@comAcast.netwrote:
siddhu wrote:
Dear experts,
A virtual function has to have an address.
Really? Why?
For the same reason every function has to have an address. You
can take its address with the & operator.

Beyond that, of course, the compiler might want the address for
some internal house keeping. In the case of a virtual function,
this is in fact very likely. (But of course, that's the
compiler's problem, not yours.)

Note that the standard guarantees that all instances of the
address of the function compare equal, even when they are taken
in different compilation units.
So if an inline virtual
function is actually inlined then in that case what does address of
this function signify?
If a function is inlined, the compiler is still free to create a body
of it _if_ it needs to take the address.
How does compiler know at compile time about
the actual object a pointer points to so that it can paste the correct
inline function in the place of function call if the function is
getting inlined?
It doesn't. The whole point of polymorphism is that the type of the
actual object is unknown until the run time.
And of course, just because you say "inline" doesn't mean that
the compiler has to generate the function inline (and vice
versa). In the case of a virtual function called through a
pointer or a reference, there's a very, very good chance that
the compiler won't inline it (although I've heard of some that
do, in some special cases).

--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

Jul 4 '07 #6
On Jul 3, 11:16 pm, "Alf P. Steinbach" <a...@start.nowrote:
* Stuart Redmann:
BTW, inline and virtual don't go together. As I recall, compilers may
even issue a warning if you try to inline virtual methods.
'inline' and 'virtual' are very compatible. A compiler that issues any
diagnostic on the combination is trash, probably pre-standard. Use
decent compilers.
I'll go even further, and say that there are two cases where I
regularly inline virtual functions, and I've never seen a
warning about it.

The first is the virtual destructor of an "interface", e.g.:

class Interface
{
public:
virtual ~Interface() {}
// Only pure virtual functions...
} ;

Since the destructor is the only function which has an
"implementation", it seems a shame to have to create a source
file just for it. (And of course, the destructor never will be
called virtually, since instances of the class can't exist.)

The second is when the concrete instance of an interface is
created by a factory function, with a different function for
each type. In such cases, the simplest solution is to define
the derived class in the factory function, and C++ requires all
functions in a local class to be defined in the class
definition, which means that they are "inline"; in this case, of
course, the function will never, ever actually be inlined.

--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

Jul 4 '07 #7
On Jul 4, 4:38 am, James Kanze <james.ka...@gmail.comwrote:
The first is the virtual destructor of an "interface", e.g.:

class Interface
{
public:
virtual ~Interface() {}
// Only pure virtual functions...
} ;

Since the destructor is the only function which has an
"implementation", it seems a shame to have to create a source
file just for it. (And of course, the destructor never will be
called virtually, since instances of the class can't exist.)
Actually, an instance of the 'Interface' class does exist, it's just
that it cannot be instantiated explicitly; however, the compiler will
create it. Furthermore, the destructor of the abstract (Interface)
class 'is' called, which is why an implementation was needed in first
place. For example:

class Interface {
public:
virtual ~Interface() = 0 {
std::cout << "virtual ~Interface()" << std::endl;
}
};

class Concrete : public Interface {
public:
~Concrete() {
std::cout << "virtual ~Concrete()" << std::endl;
}
};

void SomeFunction()
{
Concrete* concrete = new Concrete();
delete concrete;
}

....renders the following output:

"virtual ~Concrete()"
"virtual ~Interface()"


Jul 4 '07 #8
gpuchtel wrote:
On Jul 4, 4:38 am, James Kanze <james.ka...@gmail.comwrote:
The first is the virtual destructor of an "interface", e.g.:
class Interface
{
public:
virtual ~Interface() {}
// Only pure virtual functions...
} ;
Since the destructor is the only function which has an
"implementation", it seems a shame to have to create a source
file just for it. (And of course, the destructor never will be
called virtually, since instances of the class can't exist.)
Actually, an instance of the 'Interface' class does exist,
When I speak of an instance, I'm using the usual meaning: an
object with type Interface. Obviously, the derived classes will
have a sub-object of type Interface, and during construction and
destruction, there will be a moment when the dynamic type of the
object is Interface (but with the above destructor, and the
compiler provided constructors, there's no way a program can
actually see this) but there can be no fully constructed object
of type Interface.
it's just that it cannot be instantiated explicitly;
Or implicitly.
however, the compiler will create it.
When?
Furthermore, the destructor of the abstract (Interface)
class 'is' called,
But it's never called "virtually". Whenever the compiler calls
it, it knows exactly that destructor is being called. (That's
why you can declare it pure virtual.) At least read what you
are responding to.

--
James Kanze (Gabi Software) email: ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

Jul 4 '07 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

20
by: qazmlp | last post by:
My class in a header file, contains inline virtual destructor. Is this Ok? Can it cause any problems? class base { public: base() { } virtual ~base { std::cout<<"Inside virtual destructor\n";...
15
by: Dave Townsend | last post by:
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...
6
by: puzzlecracker | last post by:
Is there a case where having an "inline function" is more effective and less code i.e. code clean, but we still resort to use a regular function for this purpose? Another question, which I...
43
by: Patrick Laurent | last post by:
Hello I have a program with many many inlined template functions It is essential for the execution speed that every (or almost every) function marked as inlined, becomes really inlined by the...
6
by: RainBow | last post by:
Greetings!! I introduced the so-called "thin-template" pattern for controlling the code bloat caused due to template usage. However, one of the functions in the template happens to be virtual...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.