-2

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 ?

Sadique
  • 22,572
  • 7
  • 65
  • 91
  • 1
    You should correct the code snippet. – David Rodríguez - dribeas Jun 23 '10 at 07:28
  • 4
    Because it's homework, then instead of asking people to supply the answers, tell us what your own answers (or draft/partial answers) are, and then ask questions about or ask people to comment on or correct your answer. – ChrisW Jun 23 '10 at 07:29
  • 1
    "what you mean by because this is homework" -- You're a student: you're studying (not yet practicing) this subject. To understand this (introductory) material, it would IMO be better for you to find/understand the answer in a book (or a class/lecture), which can give sufficiently long explanations (including examples), than to get yes-or-no answers like the ones below. – ChrisW Jun 23 '10 at 08:26
  • 1
    @strut - I think _you_ are badly mistaken and ChrisW is giving _very_ useful advice. You are just refusing to listen. If you have read several C++ books, either they were bad books or you didn't pay enough attention to them. – Daniel Daranas Jun 23 '10 at 08:39

4 Answers4

4
  1. False. It just means any derived classes must implement said function. You can still provide a definition for the function, and it can be called by Base::Function().*

  2. Virtual tables are a way of implementing virtual functions. (The standard doesn't mandate this is the method, though.) When making a polymorphic call, the compiler will look up the function in the function table and call that one, enabling run-time binding. (The table is generated at compile time.)

  3. See above. Their sizes change as there are more virtual functions. However, instances don't store a table but rather a pointer to the table, so class size only has a single size increase.

  4. Sounds like you need a book.

*A classic example of this is here:

struct IBase
{
    virtual ~IBase(void) = 0;
};

inline IBase::~IBase(void) {}

This wouldn't be an abstract class without a pure virtual function, but a destructor requires a definition (since it will be called when derived classes destruct.)

Community
  • 1
  • 1
GManNickG
  • 494,350
  • 52
  • 494
  • 543
2

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.

mukeshkumar
  • 2,698
  • 3
  • 19
  • 20
  • Because IMO they're not appropriate/helpful answers to the question: incomplete, with no explanation or example. – ChrisW Jun 23 '10 at 07:44
  • @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 Jun 23 '10 at 07:52
  • @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 Jun 23 '10 at 08:15
  • @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 !! – Sadique Jun 23 '10 at 08:25
  • @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 Jun 23 '10 at 08:43
  • 1
    @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 Jun 23 '10 at 09:46
1
  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
  • 204,818
  • 23
  • 294
  • 489
1

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
  • 54,973
  • 13
  • 116
  • 224