473,767 Members | 1,873 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

virtual functions: how is direct call to base class method handled

Hi all:

How does the hardware work if you invoke a BASE::METHOD() on a DERIVED class
member? Do you still hit the v-table dynamically at run time?

Suppose you have a derived class method, but you use the :: scoping operator
to directly call the base class method.

Like this:
class BASE
{
virtual int METHOD () { return 0 };
}

class DERIVED : public BASE
{
virtual int METHOD () { return 1 };
}
I understand that if you call METHOD() on a DERIVED class member, then you
will hit the v-table, as per section 20.4 of the site below:
http://www.parashift.com/c++-faq-lit...functions.html

My question is, how does the hardware work if you invoke a BASE::METHOD() on
a DERIVED class member? Do you still hit the v-table dynamically at run
time?

Thanks in advance for your time,

ACM.
Nov 8 '05 #1
4 6080
* Amy Matlock:

How does the hardware work if you invoke a BASE::METHOD() on a DERIVED class
[object]?
The hardware works as always, and is irrelevant.

Do you still hit the v-table dynamically at run time?


Assuming a vtable-based C++ implementation, no.

A preview of a thing I'm working on:
<url:
http://home.no.net/dubjai/win32cpptut/special/pointers/preview/pointers_03__al pha.doc.pdf>.

--
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?
Nov 8 '05 #2
Alf:

Thanks for the reply. Your page is inaccessible right now, but it sounds
like you're saying the scoping operator is resolved at compile time, even
for virtual functions. So, if we modify our scenario like this:

class SUPERBASE
{
virtual int METHOD () { return 0 };
}

class BASE : public SUPERBASE
{
// no implementation of METHOD
}

class DERIVED : public BASE
{
virtual int METHOD () { return 1 };
}
Then calling BASE::METHOD() on a DERIVED class object should still NOT hit
the V-table at run time. Is it true that the proper SUPERBASE method is
bound in at compile time?

Thanks again,

ACM.
Nov 9 '05 #3
* Amy Matlock:
Alf:

Thanks for the reply. Your page is inaccessible right now,
Sorry, wrong URL.

but it sounds
like you're saying the scoping operator is resolved at compile time, even
for virtual functions. So, if we modify our scenario like this:

class SUPERBASE
{
virtual int METHOD () { return 0 };
}

class BASE : public SUPERBASE
{
// no implementation of METHOD
}

class DERIVED : public BASE
{
virtual int METHOD () { return 1 };
}
Then calling BASE::METHOD() on a DERIVED class object
Well, there are missing semicolons so the code won't compile.

Also, that member function is 'private' in BASE so can't be called from
DERIVED.

should still NOT hit
the V-table at run time. Is it true that the proper SUPERBASE method is
bound in at compile time [when you use a qualified name]?


Yes, if it were not 'private'.

Btw. it's a good idea to not use all uppercase except for macros (where
you should use only all uppercase).

That way you avoid many possible name clashes.

--
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?
Nov 9 '05 #4
Thanks, that clears it up.

Also, for others who might be interested, we found this reference:

http://cs.senecac.on.ca/~btp200/content/inclu.html

clip from the document:

"We can override the late binding with early binding wherever we wish. We
use the scope resolution operator to ensure that the virtual mechanism is
not used in any such particular case. "
Nov 10 '05 #5

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

Similar topics

3
3773
by: Daniel Graifer | last post by:
Why doesn't c++ support virtual data? It supports class data of type pointer to function (that's what virtual functions really are). In terms of implementation, why can't I have other types of data in my vtable? RTTI could be implemented this way instead of as a special case for the compiler. It would also be useful for class specific memory management implementations. -- Daniel A. Graifer
11
4368
by: santosh | last post by:
Hello, I was going through the Marshal Cline's C++ FAQ-Lite. I have a doubt regarding section 33.10. Here he is declaring a pure virtual destructor in the base class. And again defining it inline. Like this.
8
17771
by: Floogle | last post by:
how do i create a virtual == operator. I've tried the following but it's incorrect... class Interface { ... public: virtual bool operator==(const Interface& rhs)const=0;
10
7314
by: Martin Vorbrodt | last post by:
Example code in one of my books intrigues me: class B { public: B* Clone() const { B* p = DoClone(); assert(typeid(*p) == typeid(*this)); return p; }
32
4525
by: Adrian Herscu | last post by:
Hi all, In which circumstances it is appropriate to declare methods as non-virtual? Thanx, Adrian.
14
12146
by: JPRoot | last post by:
Hi I use the following syntax to have events inherited from base to child classes which works nicely (virtual and override keyword on events). But I am wondering if it is a "supported" way of using events since I never saw it used anywhere in MSDN documentation/samples?! Or it will just break when I upgrade to .NET Framework 2.x in the coming years namespace MyNamespac public delegate void MyDel() public class MyBase public virtual...
11
3451
by: ypjofficial | last post by:
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 {
5
2625
by: Dennis Jones | last post by:
Hello, I have a couple of classes that look something like this: class RecordBase { }; class RecordDerived : public RecordBase {
17
3547
by: Jess | last post by:
Hello, If I have a class that has virtual but non-pure declarations, like class A{ virtual void f(); }; Then is A still an abstract class? Do I have to have "virtual void f() = 0;" instead? I think declaring a function as "=0" is the same
0
10169
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
10013
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
9960
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
9841
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
8838
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
5280
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
5424
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3533
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2807
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.