virtual re-open

P

puzzlecracker

At what point does an object (an instance of the class that has virtual
functions) put proper function in the vtable. I dont see it how it is
run-time binding.

Base *d=new Derived (...); // let's say the both have function
// getName() declared as virtual and
//defined in the both class.

Obviously 'd' is a pointer to a an object of class D, but d considers
it as a base class and when virtual function is invoked it consults
vtable. But placing the address of the function to vtable is done at
compile time


Thanks
 
H

Howard

puzzlecracker said:
At what point does an object (an instance of the class that has virtual
functions) put proper function in the vtable. I dont see it how it is
run-time binding.

Base *d=new Derived (...); // let's say the both have function
// getName() declared as virtual and
//defined in the both class.

Obviously 'd' is a pointer to a an object of class D, but d considers
it as a base class and when virtual function is invoked it consults
vtable. But placing the address of the function to vtable is done at
compile time

(You mean 'd is a pointer to an object of class type Derived, not class D,
obviously.)

When a function call is made via that pointer, the run-time behavior is to
look up the function address, then call the function. That's runtime
binding. The fact that there exists a table of function addresses (or an
equivalent representation thereof), which is created at compile time, does
not alter the fact that the address of the function for that particular call
is not known until run time. If compile-time binding were being used, then
the address of the function would be hard-coded in the machine code at that
point in the code, instead of using a lookup.

-Howard
 
V

Victor Bazarov

puzzlecracker said:
At what point does an object (an instance of the class that has virtual
functions) put proper function in the vtable. I dont see it how it is
run-time binding.

It's done at the time of construction.

V
 
P

Pete Becker

puzzlecracker said:
At what point does an object (an instance of the class that has virtual
functions) put proper function in the vtable. I dont see it how it is
run-time binding.

Base *d=new Derived (...); // let's say the both have function
// getName() declared as virtual and
//defined in the both class.

Obviously 'd' is a pointer to a an object of class D, but d considers
it as a base class and when virtual function is invoked it consults
vtable. But placing the address of the function to vtable is done at
compile time

Base *d = new Derived(...);
d->getName(); // calls Derived::getName
d = new OtherDerived(...);
d->getName(); // calls OtherDerived::getName

So d->getName() is bound at runtime, because it calls two different
functions for the two different objects.
 
C

codigo

puzzlecracker said:
At what point does an object (an instance of the class that has virtual
functions) put proper function in the vtable. I dont see it how it is
run-time binding.

The statement below does not occur at compilation. At run-time, assuming
space is available, the object is allocated.
Base *d=new Derived (...); // let's say the both have function
// getName() declared as virtual and
//defined in the both class.

Obviously 'd' is a pointer to a an object of class D, but d considers
it as a base class and when virtual function is invoked it consults
vtable. But placing the address of the function to vtable is done at
compile time

No, at compile-time, the vtable is ready but not populated.

To prove it, write 3 Base-derived classes and initialize each element of an
array of Base pointers based on a random number generator's return
(rand()%3).
 

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

No members online now.

Forum statistics

Threads
474,202
Messages
2,571,057
Members
47,667
Latest member
DaniloB294

Latest Threads

Top