Archived

This topic is now archived and is closed to further replies.

Zeke

What is the point of virtual functions?

Recommended Posts

Ive been reading sams teach yourself C++ in 21 days and ive got to the chapter on inheritance. I already knew most of it but didnt know anything about virtual functions. After reading the part about virtual functions though Im pretty sure I understand what virtual functions do, however I dont see why you would bother using then. For example: class Mammal { //other stuff virtual void Speak(); } class Dog : public Mammal { //other stuff void Speak(); } Now I can do Mammal* pMammal=new Dog; pMammal->Speak() which will call the Speak()method in class Dog. My problem is why do this isntead of doing: Dog* pMammal=new Dog; pMammal->Speak() Because both ways call the Speak() function of the dog class. Can anyone help me understand why I should use virtual functions at all? Thanks for any help

Share this post


Link to post
Share on other sites
One of the reason for using virtual funcs would be that you could have a whole array of pointers to differing animals. But the array would look like this:

Mammal *aAnimals[100];

There could be lots of different animals pointed to in that array but you could make any of them speak by doing this:

aAnimals[4]->Speak();

Speaking animals! Madness :-)

Share this post


Link to post
Share on other sites
In fact the concept of polimorphism (I don`t know how to write this in english) is the main one in OOP (not the inheritance as some people think).

If brute force does not solve your problem....you''re not using enough!

Share this post


Link to post
Share on other sites
Here''s an example of how I use virtuals commonly:
  
class Useless {
private:
class MethodBase {
protected:
unsigned int A;
public:
inline unsigned int Get(void) const {
return A;
}
inline void Set(const unsigned int Val) {
A = Val;
}
virtual void Fish(void) = 0;
};
class MethodAlpha : public MethodBase {
void Fish(void) {
A = ((A + 5) * 3) >> 1;
}
MethodAlpha(void) {
A = 0;
}
};
class MethodBeta : public MethodBase {
void Fish(void) {
A = ((A - 5) << 3) * 4;
}
MethodBeta(void) {
A = 10000;
}
};

MethodBase *Manager;
public:
inline unsigned int Get(void) const {
return Manager->Get();
}
inline void Set(const unsigned int Val) {
Manager->Set(Val);
}
inline void Fish(void) {
Manager->Fish();
}
bool Begin(unsigned int Method) {
switch(Method) {
case 0:
Manager = new MethodAlpha;
break;
case 1:
Manager = new MethodBeta;
break;
default:
return false;
}
return (Manager != NULL);
}
void End(void) {
delete Manager;
Manager = NULL;
}
Useless(void) : Manager(NULL) {
}
~Useless(void) {
if(Manager) delete Manager;
}
};

Now I can add new methods to that class whenever I''d like to without changing the base class (this is a poor example, it looks better with more complex things). I have more uses, but that one is the funnest, so it''s all I''ll post .

[Resist Windows XP''s Invasive Production Activation Technology!]

Share this post


Link to post
Share on other sites
Um. Wow. Way confusing example. Thanks N&V.

The Reason To Use Virtual Functions (captial letters always make things sound more correct):
Use virtual functions to avoid switching on a type member. That''s really all there is to it. If you''re ever in a situation where you have a complicated class that does different things depending on what its enum Type is, you should make a base class and have virtual functions do the things for you.

Share this post


Link to post
Share on other sites