Jump to content

  • Log In with Google      Sign In   
  • Create Account

Virtual Class Functions


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
7 replies to this topic

#1 Anonymous Poster_Anonymous Poster_*   Guests   -  Reputation:

Likes

Posted 14 December 1999 - 06:13 PM


Why would I ever need to use virtual functions in my class? What
good are they?

What's the real difference between these two?


code:

class TEST_CLASS {

protected:
int num;

public:
TEST_CLASS(void);
~TEST_CLASS(void);

int Get_Num() { return num; }

};



code:

class TEST_CLASS {

protected:
int num;

public:
TEST_CLASS(void);
~TEST_CLASS(void);

virtual int Get_Num() { return num; }

};



Zack


Sponsor:

#2 mhkrause   Members   -  Reputation: 122

Like
Likes
Like

Posted 12 December 1999 - 11:31 AM

Virtual functions are how C++ implements polymorphism. This is, IMHO, the greatest feature of C++.

Here's a simple example:
class A
{
. . .
public:
virtual void Foo();
};

class B : public A
{
. . .
public:
virtual void Foo();
};

Then elsewhere, I can create a 'B' by saying:
A* pA = new B; // A pointer to 'A' can point to an A or any of its descendents.

Then, pa->Foo() will call B's Foo, instead of A's. If it weren't declared virtual, A's would be called.

This feature is useful for allowing descendent classes to override their parents behavior, for implementing an interface as a base class, then creating several implementations of that interface as descendents, etc.


#3 logistix   Members   -  Reputation: 122

Like
Likes
Like

Posted 12 December 1999 - 11:39 AM

It's useful if you're going to make polymorphic classes.

If you've done any Windows programming I'm sure that you've read that everything on the screen is a window (duh, right) Even the scroll bars, the menu bars, text boxes buttons etc. are considered windows as far as the OS is concerned. It just sends generic messages to these 'windows' and doesn't care what they do with them. As far as it's concerned they're identical, even though we can tell they're vastly different.

Although Windows isn't written in C++ the concept is the same. In game programming a good example would be some arcade game where the entities perform vastly different and custom actions (i.e. one bad guy dies and explodes into four midget versions of himself that attack you, another makes one last death throw attack at you, another just dies) where it doesn't make sense to just create flags for each unique action. You can make a virtual bad guy class:


class generic_bad_guy
{
public:
generic_bad_guy();
virtual ~generic_bad_guy(); // this should always be virtual for a base class

virtual void actionDead(void) = 0; // pure virtual function, doesn't even exist
}

And a few classes based on this:


class bad_guy_1 : public generic_bad_guy
{
public:
virtual void actionDead(void){explode();}
}

class bad_guy_2 : public generic_bad_guy
{
public:
virtual void actionDead(void){attack_one_more_time();}
}

Of course you're probably saying "i could do that anyway, with inheritance", but you couldn't do this:


generic_bad_guy* badGuys[3];

badGuy[0] = new bad_guy_1;
badGuy[1] = new bad_guy_2;
badGuy[2] = new bad_guy_3;

//here's where it gets really crazy
for(int i = 0; i < 3; i++)
{
badGuy[i]->actionDead();
}

In an array or linked list, it doesn't care what type of bad guy it really is, it just calls the appropriate function at run-time and does completely different things.

Polymorphism isn't useful for everything, but when it is you can really start to kick out the wu-tang style in your code.


#4 mordell   Members   -  Reputation: 122

Like
Likes
Like

Posted 12 December 1999 - 11:51 AM

Zack,

Well, in your example I can agree it wouldn't be much use. However, let me try to present another example and see if it helps make a little sense out of it.

//: abstract shape class
//:
class shape {

virtual void draw() = 0;
};

//: triangle class that inherits from
//: shape and implements draw()
class triangle : public shape {

void draw() { /*draw triangle */ }
};

//: square class that inherits from
//: shape and implements draw()
class square : public shape {
void draw() { /* draw square */
};


Polymorphism is provided through Inheritence, a key feature of the C++ language. Virtual functions are the backbone of polymorphism, another of the key features of C++.

With the above set of classes, polymorphism allows us to call the draw member of any class derived from shape, and have it resolve to the correct draw member.

For example, if you have a list of shape pointers, you could iterate through the list calling draw() and the correct draw member would be called for each shape type: triangle, square, or whatever.

check out:
http://members.xoom.com/zbonham/type.cpp

This is more of an example of RTTI (Run Time Type Info), however it does demonstrate what I am trying to explain here.

Any good C++ reference should give a more adequate explaination than what I could give here.

HTH,

-mordell



#5 mordell   Members   -  Reputation: 122

Like
Likes
Like

Posted 12 December 1999 - 11:53 AM


Wow! I swear there wasn't a response to the orginal post a minute ago!!



#6 Xai   Crossbones+   -  Reputation: 1459

Like
Likes
Like

Posted 12 December 1999 - 11:24 PM

In your example, don't use virtual...virtual is INEFFICIENT when it is not being used for a purpose, because it causes your program to need one extra level of indirection (one more pointer to dereference).

Virutal IS usefull however, for the reasons mentioned above, for the effect it has on RUN TIME behavior of derived classes. If you understand polymorphism, you will understand this. Otherwise, it is a good idea to read about this subject. ANY good object oriented programming book will talk about polymorphism, just go to the bookstore, open the index and look for "polymorphism" or "inheiritance".


#7 kressilac   Members   -  Reputation: 110

Like
Likes
Like

Posted 13 December 1999 - 04:45 AM

I have to agree with Xai. Just like drinking and everything else in the world, practice moderation and you will get the maximum benefit for the least drawback. Virtual functions do cause additional overhead in that the executable must keep tables of pointers so that base classes can call the correct function. This overhead isn't much but it is significant.

I used to have a Java book that talked about the expense of operations such as polymorphism, casting, etc etc. The relationship is staggering. For instance casting is some crazy number of times more processor expensive than instantiating the actual type. I believe calling a pA->foo(); where pA is a B causes pA to be cast into a pB and then passed as a *this to foo() of a B. Please correct me if I am wrong. In this case the cost of polymorphism is the cost of the cast and the additional dereference Xai mentioned.

Kressilac

ps This is the major drawback to polymorphism. If we could get around it IMHO polymorphism would be the greatest tool C++ delivered. Think about how much more expensive traversing a list of pA pointing to a B would be versus a list of pB pointing to B. The more elements you have the more casting up and down the class hierarchy occurs. Expensive.
------------------
Derek Licciardi

[This message has been edited by kressilac (edited December 13, 1999).]


#8 mhkrause   Members   -  Reputation: 122

Like
Likes
Like

Posted 14 December 1999 - 06:13 PM

No cast should be done during a virtual function call. It's just a table lookup, which isn't too bad if it is in the cache.

Also, if a function is virtual, but the type of the pointer is known at run time (it's not a pointer or reference) then no runtime overhead should incur, assuming the compiler is decent.





Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS