473,508 Members | 2,330 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

problem on inline virtual function.

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
3 1680
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

20
5931
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
10604
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
322
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...
1
1676
by: TheOne | last post by:
I have two classes: class OntologyParser { ... protected: virtual void startElement(void *userData, const char *name, const char **atts); virtual void endElement(void *userData, const char...
6
3983
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
3910
by: Lokkju | last post by:
I am pretty much lost here - I am trying to create a managed c++ wrapper for this dll, so that I can use it from c#/vb.net, however, it does not conform to any standard style of coding I have seen....
8
2985
by: siddhu | last post by:
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...
5
1905
by: sunil | last post by:
Hello, I have a class deriving from a class that provides ability to serialize/deserialize objects over the network. There are two classes Requests (sent from client to server) Response(sent from...
5
2531
by: .rhavin grobert | last post by:
if you have class Base { virtual inline bool foo() {return false;}; }; class Derived: public Base { virtual bool foo(); };
0
7342
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7410
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...
1
7067
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
5650
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,...
1
5060
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...
0
3215
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
1570
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
774
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
440
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.