473,753 Members | 8,077 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

same overhead in calling virtual and non virtual member function...?

Hello All,

So far I have been reading that in case of a polymorphic class ( having
at least one virtual function in it), the virtual function call get
resolved at run time and during that the vtable pointer is made use
of..

eg.
class one
{
virtual void fun1(){ cout<<"one::fun 1";} //This is a virtual
function.
void fun2(){ cout<<"one ::fun2";}//Not a virtual function.

};

int main()
{
one * o = new one;
o->fun1();
o->fun2();
delete o;
o= NULL;
return 0;
}

so when the virtual function gets called through the base class poitner
the call actually gets expanded to the code like this
o->vfptr[0]();
My confusion is how the call to the non virtual function (here fun2
)gets resolved in polymorphic class?
When does the compiler decide to look into the vtable and when not to
look?
As in this scenario I strongly feel that, every time the compiler has
to look into the vtable irrespective of the virtuality or non
virtuality of the function.If it finds the function entry in the vtable
then it calls the function from there otherwise if it doesn't find any
entry into the vtable it looks for the non virtual function and then
execute the fuction code?
So in other words whenever your class is polymorphic , we have to deal
with this overhead always whether the class user calls the virtual or
non virtual function...

Can anyone please clarify it..?

Thanks and Regards,
Yogesh Joshi
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.m oderated. First time posters: Do this! ]

Sep 8 '06
11 3449
* yp*********@ind iatimes.com:
As in this scenario I strongly feel that, every time the compiler has
to look into the vtable irrespective of the virtuality or non
virtuality of the function.If it finds the function entry in the vtable
then it calls the function from there otherwise if it doesn't find any
entry into the vtable it looks for the non virtual function and then
execute the fuction code?
No.

First off, the language does not require a vtable, although in practice
all implementations you're likely to encounter are vtable-based.

If a function is non-virtual the function address (where its code
resides in memory) is known at compile time, at least symbolically.
That's all you need to issue a call to the function. When a function is
virtual and is called virtually in the source code, the function address
is not necessarily known at compile time, because it then depends on the
object the function is called on, so in that case the compiler may have
to insert code that looks up the function address in the vtable
specified by the object.

<netiquette>
By the way, please don't cross-post to clc++ and clc++m.

Generally you get answers faster in clc++, but when you're crossposting
to a moderated group every article is held up in all groups until it's
accepted (or rejected), so you don't get faster answers: at best you
gain a somewhat broader audience, but some answers you'd otherwise have
had may not be posted (because some posters don't care to wait for the
turnaround of a moderated group), some answers you'd otherwise have had
may be rejected, you run a much higher chance of thread drift, and
you're annoying and worse those who unwittingly reply to your clc++
article expecting their response to show up more or less immediately.

Multiposting -- posting separately -- is an even worse idea, because
the poster then Really Annoys those who reply in one group only to find
the same article posted separately in another group.

A month or two ago there was a surge of multiposting in clc++m. I
generally think of those who multi-post without reason as being either
idiots or non-caring egoists or both (unless they are clearly
first-timers on the net), and I would not be surprised if that's how
most others also view them. Happily multi-posting is not what you did
here, but don't do it. ;-)
</netiquette>

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.m oderated. First time posters: Do this! ]

Sep 10 '06 #11
Greg Herlihy wrote:
yp*********@ind iatimes.com wrote:
So far I have been reading that in case of a polymorphic class ( having
at least one virtual function in it), the virtual function call get
resolved at run time and during that the vtable pointer is made use
eg.
of..
class one
{
virtual void fun1(){ cout<<"one::fun 1";} //This is a virtual
function.
void fun2(){ cout<<"one ::fun2";}//Not a virtual function.

};
so when the virtual function gets called through the base class poitner
the call actually gets expanded to the code like this
o->vfptr[0]();
Yes, at least for C++ compilers that use vtables to implement
virtual functions. But the general point is that one function
call to a virtual method in the source code can - at runtime -
execute any of several distinct methods (based on the runtime
type of the object) each and every time. So calling a virtual
method requires run-time decision-making which is not needed
when calling non-virtual methods or global functions.
My confusion is how the call to the non virtual function
(here fun2)gets resolved in polymorphic class? When does
the compiler decide to look into the vtable and when not to
look?
The compiler generates "lookup-code" when the method being called has
been declared virtual, otherwise it generates a direct call to the
method as determined by the object's static type.
More strictly speaking: if the compiler doesn't know at compile
time which function to call, it must generate code to determine
this at run-time. By definition, if the function isn't virtual,
the compiler does know this at run time. If the function is
virtual, it may or may not know it, depending on how good it is.
Almost all compilers will know it if the call is through an
actual object; if the call is through a reference or a pointer,
it depends on the quality of the optimizer.

--
James Kanze (Gabi Software) email: ka*********@neu f.fr
Conseils en informatique orientée objet/
Beratung in objektorientier ter Datenverarbeitu ng
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.m oderated. First time posters: Do this! ]

Sep 10 '06 #12

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

Similar topics

5
4656
by: Praveen Srinivasan | last post by:
Hi all, Are there any important issues that one should be aware of when calling C++ functions from C? In particular, I'm passing a function pointer to a C routine in a library, and in that function I do C++ type stuff, e.g. utilizing operator overloading. Is that reasonable, or are there some pitfalls I need to watch out for? Thanks, Praveen Srinivasan
3
12111
by: Roy Yao | last post by:
Hello, I need to pass a pointer to a callback function to the lower level modules. But the function is thought to be a virtual member one. How can I get the real address of the virtual member function?
3
2524
by: Philippe Guglielmetti | last post by:
Look at these few lines of code: class A { public: virtual void f() { cout << "A";}}; class B : public A{public: static void f() { cout << "B"; }}; class C : public B{public: void f() { cout << "C"; }}; // virtual or not ? that's the question... int main(int, char**) { A* x; A a; B b; C c;
2
1918
by: William Payne | last post by:
Hello, consider these following two classes. A base class, class MDIChildWindow, and a class inherting from that base class, class Document. In the static base member function callback() I obtain a pointer to the child class and call the function on_mdiactivate() using this pointer. For some reason, the program executes MDIChildClass::on_mdiactivate() and not Document::on_mdiactivate(). Why? on_mdiactivate() is a virtual function in...
3
11441
by: scott | last post by:
hi all, hope some one can help me. Ill try and explain what im trying to do as best i can. i have a parent class that has a vertual function, lets call it virtual int A(). That vertual function does somthing that must be done. This meens that when a child class inherits the class and creates its own vertual int A() the parent class must also be called. the prob is i can not use the base class name and then its functino name after it...
6
2248
by: BigMan | last post by:
Is it safe to call nonvirtual member functions from ctors and dtors? What about virtual ones?
1
1565
by: question | last post by:
I want to know incase there is any performance difference or overhead in calling a base class method and a derived class method. Basically I am talking about simple method that is not overridden nor virtual. If I declare a method in the base class say M1() and another in derived class M2(). Then I make a derived class object derived. I then invoke these: derved.M1() derived.M2()
3
1916
by: marcwentink | last post by:
Say I have a class A, and a class B that inherits from A. Now A (and B) has a virtual destructor and a virtual function F(); If I now make these statements A* ptrA = new B; ptrA->F(); delete ptrA then in the statement ptrA->F(), by means of the polymorph behavior,
7
3090
by: eric | last post by:
hello i'm confused by an example in the book "Effective C++ Third Edition" and would be grateful for some help. here's the code: class Person { public: Person(); virtual ~Person(); // see item 7 for why this is virtual ...
0
8896
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9653
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
9451
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...
0
9333
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8328
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...
1
6869
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6151
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
3395
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
3
2284
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.