473,666 Members | 2,165 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 1690
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
5981
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"; } // Other members
15
10628
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 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
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 previously posted but answers were very opinioned and not conclusive - Likely due to somewhat ambiguous description in the first place. Sorry for repost!
1
1681
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 *name);
6
3998
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 as well. To support thin-template, I need to make virtual function as inline. Now, I know that compiler would generate an out-of-line copy when it
0
3932
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. It is almost like it is trying to implement it's own COM interfaces... below is the header, and a link to the dll+code: Zip file with header, example, and DLL:...
8
2995
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 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?
5
1924
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 server to client), these two classes need to implement a pure virtual method called dispatch(), however only client needs to implement Response::dispatch() and only server needs to implement Request::dispatch(). I am planning to build a library...
5
2536
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
8454
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8878
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8785
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8560
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
7389
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
4200
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4372
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2776
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 we have to send another system
2
1778
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.