Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > calling virtual function from base class

Reply
Thread Tools

calling virtual function from base class

 
 
Phui Hock
Guest
Posts: n/a
 
      09-07-2004
"When you make a virtual function call through a base-class pointer
(that is, when you make a polymorphic call), the compiler quietly
inserts code to fetch the VPTR and look up the function address in the
VTABLE..."

Can anyone tell me how the VPTR of the derived class is fetched? Where
can I get more information bout this?

Thank u

 
Reply With Quote
 
 
 
 
John Harrison
Guest
Posts: n/a
 
      09-07-2004

"Phui Hock" <(E-Mail Removed)> wrote in message
news:chkseq$(E-Mail Removed)...
> "When you make a virtual function call through a base-class pointer
> (that is, when you make a polymorphic call), the compiler quietly
> inserts code to fetch the VPTR and look up the function address in the
> VTABLE..."
>
> Can anyone tell me how the VPTR of the derived class is fetched? Where
> can I get more information bout this?
>
> Thank u
>


'Inside the C++ Object Model' by Stanley Lippman.

john


 
Reply With Quote
 
 
 
 
Mike Wahler
Guest
Posts: n/a
 
      09-07-2004

"Phui Hock" <(E-Mail Removed)> wrote in message
news:chkseq$(E-Mail Removed)...
> "When you make a virtual function call through a base-class pointer
> (that is, when you make a polymorphic call), the compiler quietly
> inserts code to fetch the VPTR and look up the function address in the
> VTABLE..."
>
> Can anyone tell me how the VPTR of the derived class is fetched? Where
> can I get more information bout this?


The book John cited does indeed give useful insight into
the design of C++.

But note that the actual mechanics of e.g. polymorphism is
completely implementation-specific. Each might implement it
in its own way. The only language requirement is that a
particular *behavior* is the result. The language does not
define things such as 'v-tables' or 'v-pointers'.

-Mike


 
Reply With Quote
 
E. Robert Tisdale
Guest
Posts: n/a
 
      09-07-2004
Phui Hock wrote:

> "When you make a virtual function call through a base-class pointer
> (that is, when you make a polymorphic call),
> the compiler quietly inserts code
> to fetch the VPTR and look up the function address in the VTABLE..."
>
> Can anyone tell me how the VPTR of the derived class is fetched?
> Where can I get more information bout this?


The VPTR *is* a pointer to the VTABLE for the derived class
if the object was created using a constructor for the derived class.
 
Reply With Quote
 
JKop
Guest
Posts: n/a
 
      09-07-2004
Phui Hock posted:

> "When you make a virtual function call through a base-class pointer
> (that is, when you make a polymorphic call), the compiler quietly
> inserts code to fetch the VPTR and look up the function address in the
> VTABLE..."
>
> Can anyone tell me how the VPTR of the derived class is fetched? Where
> can I get more information bout this?
>
> Thank u


One thing I think you should realize: If you have a scenario where you have
a certain function:

void EatPoo()

And this function is NON-virtual in the base class:

class Base
{
public:

void EatPoo()
{
cout << "Base::EatPoo";
}
};

And then you have a derived class as so:

class Derived : public Base
{
public:

virtual void EatPoo()
{
cout << "Derived::EatPoo";
}
};

That now when you do the following:

int main()
{
Derived k;

Base& r = k;

r.EatPoo();
}

It's "Base::EatPoo" that will be called.


-JKop

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
calling virtual function results in calling function of base class... Andreas Lagemann C++ 8 01-10-2005 11:03 PM
calling virtual function results in calling function of base class ... tiwy C++ 0 01-09-2005 11:17 PM
Re: calling virtual function from base class Phui Hock C++ 3 09-07-2004 09:06 PM
Virtual function 'BasicMidReader::~BasicMidReader()' conflicts with base class 'base 'TMemoryStream' tomek C++ 2 12-01-2003 06:31 AM
Virtual function 'BasicMidReader::~BasicMidReader()' conflicts with base class 'base 'TMemoryStream' tomek C++ 3 11-30-2003 12:18 AM



Advertisments