![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
|
|
#1 |
|
Newbie
Join Date: May 2006
Posts: 1
Rep Power: 0
![]() |
could sum1 plz explain the comparison between ordinary member functions & polymorphic member functions.
![]() |
|
|
|
|
|
#2 |
|
Programming Guru
![]() Join Date: Jun 2005
Location: elemental plane
Posts: 1,429
Rep Power: 5
![]() |
Virtual functions r 4 l8 binding.
For goodness sake, please write in plain English.
__________________
"Employ your time in improving yourself by other men's writings, so that you shall gain easily what others have labored hard for." -- Socrates |
|
|
|
|
|
#3 | |
|
SEXY SHOELESS GOD OF WAR!
![]() Join Date: Jun 2005
Location: Wet west coast of Canada
Posts: 1,194
Rep Power: 5
![]() |
Quote:
Here is an example to make things a little clearer, hopefully: #include <iostream>
class fruit
{
public:
virtual void printName(void);
};
void fruit::printName(void)
{
std::cout << "I am a generic fruit.\n";
}
class apple:public fruit
{
public:
void printName(void);
};
void apple::printName(void)
{
std::cout << "I am an apple.\n";
}
int main(int argc, char *argv[]);
void printFruitName(fruit &f);
int main(int argc, char *argv[])
{
fruit foo;
apple bar;
printFruitName(foo);
printFruitName(bar);
return 0;
}
void printFruitName(fruit &f)
{
f.printName();
}I am a generic fruit. I am an apple. The way in which it is implemented can vary from one language to another, but in C++, it is generally (always?) done with a 'vtable', which is a table of pointers to the virtual (hence the 'v') methods. When objects are constructed, the table entry (or entries) are filled in with a pointer to the actual method. This makes the code slightly longer and slower, which is why virtual methods are not the default (there is no reason to accept a performance hit unless it is required). However, virtual methods offer lots of flexibility in your programming, and allow you to write less code (or reuse more existing code) than would otherwise be possible, and when you need to determine the method to call at run-time, doing it through a technique like a vtable is the most efficient means. The other option is some immense switch() statement (or similar construct) that examines a 'type' data member set by the constructor, and this is obviously a bad idea. [edit] Oh yeah, like nnxion said, English is a good idea. Butchering the language for 'leet-speak' just makes you look silly, stupid, or annoying. [/edit]
__________________
And once again, Probability proves itself willing to sneak into a back alley and service Drama as would a copper-piece harlot. - Vaarsuvius, Order of the Stick |
|
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|