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

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

P: n/a
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
Share this Question
Share on Google+
4 Replies


P: n/a
* 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__alpha.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

P: n/a
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

P: n/a
* 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

P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.