tags:

views:

114

answers:

4

I need some answers to basic questions. I'm lost again. :(

q1 - Is this statement valid:
Whenever we define the function to be pure virtual function, this means that function has no body.

q2 - And what is the concept of Dynamic Binding? I mean if the Compiler optimizes the code using VTABLEs and VPTRs then how is it Run-Time Polymorphism?

q3 - What are VTABLES AND VPTRs and how do their sizes change?

q4 - Please see this code:

class base
{
    public:
        virtual void display()
        {
            cout<<"Displaying from base";
        }
};

class derived:public base
{
    public:
        void display(){cout<<"\nDisplaying from derived";}
};

int main()
{
    base b,*bptr;
    derived d;
    bptr=&b;
    bptr->display();
    bptr=&d;
    bptr->display();
} 

Output:

Displaying from base
Displaying from derieved

Please can somebody answer why a pointer of base class can point the member function of a derived class and the vice-versa is not possible, why ?

+2  A: 

1) Not necessarily. There are times when you provide body for pure virtual functions

2) The function to be called is determined at run time.

hype
Even though it's incomplete, nothing here is wrong. Why -1?
GMan
I wonder why this was downvoted, as it is correct.
David Rodríguez - dribeas
Because IMO they're not appropriate/helpful answers to the question: incomplete, with no explanation or example.
ChrisW
@ChrisW: If you don't think they add value --but at the same time they don't take it away as it is not incorrect-- just leave it at 0. The net effect is that I and probably Gman (or someone else) saw an answer that was not incorrect with a negative number and we both upvoted while we saw it negative. The net effect has been that the answer now has +1 as the net result.
David Rodríguez - dribeas
@David - A downvote means that in your opinion an answer is "Not Helpful" or "Not Useful": it's not just about whether the answer is "true"; and, I thought that this answer wasn't helpful. Also "Gman (or someone else)" vote isn't up to me.
ChrisW
@ChrisW - When you yourself havent posted an answer why are you undermining other's!! You said this is some kind of homework which i posted. It is weird to think like that, my questions are regarding clarity and not solving some stupid assignments which have been a Piece 'a Cake for me !!
strut
@strut Downvoting != undermining. Also, this is not a forum. Please refrain to discuss the _actions_ that a particular user did. Comments are intended to clarify/discuss the content of each answer.
Daniel Daranas
@ChrisW, I was just trying to make the point that in many cases downvotes have the opposite effect: people will try to compensate, and in many cases that will over-compensate for the downvote. It is just an opinion, but the downvote did not actually help your point of *unhelpfulness*, but on the contrary it had the opposite effect. I did not intend, in any case, to tell you when to upvote/downvote, just express my own experience in the past. If you understood that, my apologies.
David Rodríguez - dribeas
+1  A: 
  1. False. It only means that derived classes must implement the method and that the method definition (if present) at that level will not be consider an override of the virtual method.
  2. The vtable is implemented at compile time, but used at runtime. The compiler will redirect the call through the vtable, and that depends on the runtime type of the object (a pointer to base has static type base*, but might point to an object of type derived at runtime).
  3. vptrs are pointers to an vtable, they do not change size. vtables are tables of pointers to code (might point to methods or to some adapter code) and have one entry for each virtual method declared in the class.

After the edit in the code:

The pointer refers to an object of type base during the first call, but it points to an object of type derived at the second call position. The dynamic dispatch mechanism (vtable) routes the call to the appropriate method.

A common implementation, which may help you understand is that in each class that declares virtual functions the compiler reserves space for a pointer to a virtual table, and it also generates the virtual table itself, where it adds pointers to the definition of each virtual method. The memory layout of the object only has that extra pointer.

When a derived class overrides any of the base class methods, the compiler generates a different vtable with pointers to the final overriders at that level. The memory layout of both the base and the derived class coincide in the base subobject part (usually the beginning), but the value of the vptr of a base object will point to the base vtable, while the value of the vptr in the derived object will point to the derived vtable.

When the compiler sees a call like bptr->display(), it checks the definition of the base class, and sees that it is the first virtual method, then it redirects the call as: bptr->hidden_vptr[0](). If the pointer is referring to a real base instance, that will be a pointer to base::display, while in the case of a derived instance it will point to derived::display.

Note that there is a lot of hand-waving in this answer. All this is implementation defined (the language does not specify the dispatch mechanism), and in most cases the dispatch mechanism is more complex. For example, when multiple inheritance takes place, the vtable will not point directly to the final overrider, but to an adapter block of code that resets the first implicit this parameter offset, as the base subobject of all but the first base are unaligned with the most derived object in memory --this is well beyond the scope of the question, just remember that this answer is a rough idea and that there is added complexity in real systems.

David Rodríguez - dribeas
+4  A: 
GMan
+1  A: 

Is this statement valid

Not exactly: it might have a body. A more accurate definition is "Whenever we define a method to be pure virtual, this means that the method must be defined (overriden) in a concrete subclass."

And what is the concept of Dynamic Binding? I mean if the Compiler optimizes the code using VTABLEs and VPTRs then how is it Run-Time Polymorphism?

If you have an instance of a superclass (e.g. Shape) at run-time, you don't/needn't necessarily know which of its subclasses (e.g. Circle or Square) it is.

What are VTABLES AND VPTRs and how do their sizes change?

There's one vtable per class (for any class which has one or more virtual methods). The vtable contains pointers to the addresses of the class's virtual methods.

There's one vptr per object (for any object which has one or more virtual methods). The vptr points to the vtable for that object's class.

The size of the vtable increases with the number of virtual functions in the class. The size of the vptr is probably constant.

Please can somebody answer why a pointer of base class can point the member function of a derived class and the vice-versa is not possible, why ?

If you want to invoke the base class function then (because it's virtual, and the default behaviour for virtual is to call the most-derived version via the vptr/vtable) then you have to say so explicitly, e.g. like this:

bptr->base::display(); 
ChrisW