| rmxhaha (26) | ||
http://en.wikipedia.org/wiki/Virtual_method_table
I really want to know why is that so ? I mean If all the the virtual function has been overriden in derived class then why does it need room for a pointer to a function. Basicly there could only be 1 possiblity. | ||
|
|
||
| Athar (4379) | |||
Well, take this example:
There's more than one possibility for speak() now, isn't there? | |||
|
Last edited on
|
|||
| rmxhaha (26) | |
|
OOk, I think I get it but what if I don't want that kind of thing Should I use copy and paste instead of inheritance because I don't think I can override a non-virtual function | |
|
|
|
| Athar (4379) | |||
What kind of thing?
I don't really know what you mean, but the answer is no in any case. | |||
|
|
|||
| kbw (5374) | |
|
You have to decide what paradigm you're using; procedural programming, abstract data types, object oriented programming. Once you've decided what parts of the program are using what, you have to follow the rules of that paradigm. If you are not doing object oriented programming, then don't do it. The vtable mechanism is part of the object oriented support in C++. If you're not using OO, you won't be using such things. | |
|
|
|
| rmxhaha (26) | |||||
|
Suppose I am trying to not waste memory I have a class which is inherited trough so many class. Let's say I have a class that have 50 virtual function. That makes it 200 Byte big without anything else... Let's call it "Cat" All the function from the where Cat has inherit has been overriden. suppose anywhere in the code there won't be other than
There won't be
| |||||
|
|
|||||
| Athar (4379) | |
|
Each object instance has at most one pointer to the virtual function table of its class. You're not wasting any memory. Besides, if you think you don't need virtual functions, then just don't use them. | |
|
Last edited on
|
|
| rmxhaha (26) | |
| So if it's 5 inheritance deep it would only waste 5 pointer ??? is that what you are trying to say ?? | |
|
|
|
| Athar (4379) | |
| No, it's at most one pointer per object (at least as long as no multiple inheritance is involved). It is irrelevant how deep your inheritance tree is. | |
|
Last edited on
|
|
| rmxhaha (26) | |
| Oh Ok Thanks for your help | |
|
|
|