By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,388 Members | 1,823 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,388 IT Pros & Developers. It's quick & easy.

problem on inline virtual function.

P: n/a
Dear All,

If I define a virtual function to be inline, is it really inline? Or it
is inline in some cases, and not in other cases. Would you please help
me to look at the following case.

struct A
{
int i;
A() {i = 0;}
virtual void Add() {i++;}
};

struct B : public A
{
virtual void Add() {i += 2;}
};

int main()
{
A a;
a.Add(); // inline ?

B b;
b.Add(); // inline ?

A *pa = new A();
pa->Add(); // not inline?

A *pb = new B();
pb->Add(); // not inline?

return 0;
}

Thanks!

Shuisheng

Oct 3 '06 #1
Share this Question
Share on Google+
3 Replies


P: n/a
shuisheng wrote:
Dear All,

If I define a virtual function to be inline, is it really inline? Or it
is inline in some cases, and not in other cases. Would you please help
me to look at the following case.
It depends on the compiler, however it is nigh impossible for a compiler
to inline (as in inline the generated code) a virtual funtion.
Oct 3 '06 #2

P: n/a
shuisheng wrote:
If I define a virtual function to be inline, is it really inline?
No function you *declare* 'inline' is really inline. There is no way
in the language to find out. The compiler will do what it wants.
Or
it is inline in some cases, and not in other cases. Would you please
help me to look at the following case.

struct A
{
int i;
A() {i = 0;}
virtual void Add() {i++;}
};

struct B : public A
{
virtual void Add() {i += 2;}
};

int main()
{
A a;
a.Add(); // inline ?
Yes, very likely.
>
B b;
b.Add(); // inline ?
Yes, very likely.
>
A *pa = new A();
pa->Add(); // not inline?

A *pb = new B();
pb->Add(); // not inline?

return 0;
}
The last two cases, I don't know, but I suspect that only the
last time the call will be non-inlined (polymorphic). Both
cases are possible to track since 'pa' and 'pb' are local objects,
and the compiler _may_ be written to track those things, but no
guarantees can be given.

Ask your compiler to generate assembly code for you and look.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Oct 3 '06 #3

P: n/a
shuisheng wrote:
Dear All,

If I define a virtual function to be inline, is it really inline? Or it
is inline in some cases, and not in other cases. Would you please help
me to look at the following case.

struct A
{
int i;
A() {i = 0;}
virtual void Add() {i++;}
};

struct B : public A
{
virtual void Add() {i += 2;}
};

int main()
{
A a;
a.Add(); // inline ?

B b;
b.Add(); // inline ?

A *pa = new A();
pa->Add(); // not inline?

A *pb = new B();
pb->Add(); // not inline?

return 0;
}

Thanks!

Shuisheng
I t d e p e n d s o n y o u r c o m p i l e r .
" i n l i n e " i s j u s t a h i n t ,
i t i s n o t a l a w .

----== Posted via Newsfeeds.Com - Unlimited-Unrestricted-Secure Usenet News==----
http://www.newsfeeds.com The #1 Newsgroup Service in the World! 120,000+ Newsgroups
----= East and West-Coast Server Farms - Total Privacy via Encryption =----
Oct 3 '06 #4

This discussion thread is closed

Replies have been disabled for this discussion.