Virtual functions? Inheritance? Why use them???

Started by
26 comments, last by Ghostface 22 years, 8 months ago
Huh? I thought for sure there was a way to override base class functions without having to do all that messy stuff...



Edited by - Ghostface on July 31, 2001 10:34:36 AM
------------------------------"I do not feel obliged to believe that the same God who has endowed us with sense, reason, and intellect has intended us to forgo their use. " - Galileo Galilei
Advertisement
I''ve always heard that called "hiding". In those examples the only thing that is the same is the function signature. The compiler doesn''t see the connection that we make in our minds, other than it has to hide one, just like it would hide a variable with the same name if a new one is introduced in a scope.
Ghostface, if you are still unsure what the benifits to virtual functions are, then hopefully the thread below will clear things up.

http://www.gamedev.net/community/forums/topic.asp?topic_id=23993

Also, try searching the forums on questions like these, as they''ve been asked and answered many times before.

Hope this helps!


- Houdini
- Houdini
Virtual functions allow the correct version of a function to be called based on the type that is pointed to as opposed to the type of the pointer variable itself. Virtual functions use late-biding to determine what to call, and regular functions use early-binding, when the program is compiled. since we as programmers only have control before the code is compiled, having programing features that allow the program to make dynamic decisions about what functions to call is very advantegeous.
yeah, i was basically going to say what zipster said. all you have to do is declare one pointer, for the base class, and just have it reference the derived classes so that it can call those functions for the derived class, instead of have to create pointers for each and every class.

if you tried to override functions and tried to call functions for the derived class, using a base class pointer, it wouldn''t work, it would call the base calss funcion.
I would first like to thank all of you for your posts and your patience.

Second, I believe that I have a fairly good grasp on virtual functions now. I suppose that I will only find a good use for them in the future...howeverm feel free to post if you feel as though something has been left out.
------------------------------"I do not feel obliged to believe that the same God who has endowed us with sense, reason, and intellect has intended us to forgo their use. " - Galileo Galilei
ok, ghostface, I'm noticing that you either don't use pointers too much, don't know how to use them, or don't see the point, I'm not sure which. I'm going to pull a quote out of the book that got me off the ground, Visual C++ 6 For Dummies:

quote:If I Never Use Pointers to Objects, Can I (ignore virtual functions)?

Yup.


so, if you haven't figured out pointers or don't use them yet, you don't have any reason to worry about whether a function is virtual or not.
Now, to get a couple other things out of the way...
From this point on, I'm going to assume you at least know what pointers are and how to make one, just not their usefulness.

Say you have a class which has a whole shitload of huge variables (say, a couple of 10000000 element arrays) called BigClass. Say you make an object of this class called Data, and pass it into a function with the following prototype:
void MyFunc(BigClass d);
Now, passing Data into a function as a normal variable will tell the computer to make a copy of Data, and send the copy to the function, which will slow down the computer during the copying and eat up memory like nobody's business.
With me so far? Good.
Now then, say, instead of making a normal variable like this:
BigClass Data;
You used pointers instead, like this:
BigClass* Data;
Data = new BigClass();
Also, we change MyFunc to take a pointer to a BigClass, instead of an actual variable:
void MyFunc(BigClass* d);
Now, if you were to pass Data into MyFunc this time, it would pass along the address of the required data instead of making a copy of the data to work with, which is a whole helluva lot faster.

And thus my little speech ends. If you're still confused (or I left out something), feel free to make another post (I'd let you e-mail, except my box is clogged enough as it is )

-Normie

[EDIT]
Heh, just realized my post is a lottle off topic on your original question...I'd delete this, except I really think you should know these things.


Edited by - normie on August 1, 2001 2:08:47 AM

Edited by - normie on August 1, 2001 2:13:27 AM
I am a devout follower of the"Lazy Programmer's Doctrime"(tm)...and I'm damned proud of it, too!-----"I came, I saw, I started makinggames." ... If you'll excuseme, I must resume my searchfor my long lost lobotomy stitches.
quote:Original post by brad_beveridge
Now lets say that you have a function called CallMe, which prints out "B" for Base classes & "D" for Derived classes. If you DON''T make it virtual and run through the array calling bpointer->CallMe(); you will always get "A" printed out, even if the whole array is Derived classes. The reason is an optimisation. Because C++ compiler KNOWS that the whole array are bpointers & it KNOWS that your function is NOT virtual, it will ALWAYS call the Base::CallMe function, it has no need to look anywhere else. So to make it examine which function should be called (at run time) you have to make it virtual, then it won''t just look at the static declaration in code to find the kind of object it is pointing at, it will actually ask - am I a Base or a Derived? And then run whichever function.
Also I would say "understand pointers & references VERY well". If you don''t know what they''re for, here''s one example. It reduces stack space when you pass parameters (only a 32bit pointer has to be passed, rather than a huge object), and it prevents nasty copy constructors being called. For speed you want to avoid copy constructors & tempory objects & the = operator like the plague. You also don''t want to make you arithmetic operators return tempory objects, ''cos it slows things down. If you don''t under stand, mail me at
brad_(take this out - I hate spam)beveridge@hotmail.com

Cheers
Brad



Woah, that was an excellent reply. I think that just answered everything just nice and simple.


- Just another sad bastard

This topic is closed to new replies.

Advertisement