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

A

Amy Matlock

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-lite/virtual-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.
 
A

Amy Matlock

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.
 
A

Alf P. Steinbach

* 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

Amy Matlock

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. "
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top