Jump to content
  • Advertisement
Sign in to follow this  
Avont29

virtual member functions and try/catch

This topic is 4759 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

can someone please explain those, im just not getting it, i get try catch a little, but the throw thing is confusing, and virtualmember functions don't make sense, what does OVERRIDE mean, please help me thanks in advance

Share this post


Link to post
Share on other sites
Advertisement
Quote:
Original post by Avont29
... virtualmember functions don't make sense, what does OVERRIDE mean, please help me

Virtual member functions allow you to call a derived class' function with a pointer or reference to the base class. For a simple demonstration:
#include <iostream>
#include <vector>
using std::cout;
using std::endl;

class subsystem {
public: virtual void init() {;} // calls to these functions will be forwarded
virtual void quit() {;} // to any derived class that implements it.
virtual ~subsystem() {;} // <- same here
};

class video : public subsystem {
public: void init() { cout << "subsystem init: video" << endl; return; }
void quit() { cout << "subsystem quit: video" << endl; return; }
~video() { cout << "deleting video" << endl; return; }
};

class input : public subsystem {
public: void init() { cout << "subsystem init: input" << endl; return; }
void quit() { cout << "subsystem quit: input" << endl; return; }
~input() { cout << "deleting input" << endl; return; }
};

int main() {

cout << "create a vector of subsystem pointers ..." << endl << endl;
std::vector< subsystem * > subs; // notice we're storing pointers to base class here

// allocate and store pointers to derived subsystems
video * vid = new video;
input * inp = new input;
subs.push_back( vid );
subs.push_back( inp );

cout << "polymorphically call the derived subsystems' init():" << endl;
std::vector< subsystem * >::iterator sub = subs.begin();
while( sub != subs.end() ) {
(*sub++)->init(); // this call gets transferred to video::init() and input::init()
}

cout << "polymorphically call the derived subsystems' quit(), then delete subsystem:" << endl;
sub = subs.begin();
while( sub != subs.end() ) {
(*sub)->quit(); // this call gets transferred to video::quit() and input::quit()
delete (*sub++); // this call gets transferred to video::~video() and input::~input()
}

return 0;

}



EDIT: cleaned up code a bit

[Edited by - stylin on October 9, 2005 3:14:16 AM]

Share this post


Link to post
Share on other sites
Quote:
Original post by stylin
Quote:
Original post by Avont29
... virtualmember functions don't make sense, what does OVERRIDE mean, please help me

Virtual member functions allow you to call a derived class' function with a pointer or reference to the base class. For a simple demonstration:*** Source Snippet Removed ***

EDIT: cleaned up code a bit




so when you call one of the video functions like void init, it actually calls the other one in the subsystem baseclass?

Share this post


Link to post
Share on other sites
Quote:
Original post by Avont29
so when you call one of the video functions like void init, it actually calls the other one in the subsystem baseclass?


No, it's the reverse. The derived class overrides the base class function, so when you call the base class function, it gets rerouted to the derived class' version. Making a function virtual means that it can be replaced by a function in a derived class. That means that the derived class can implement it's own specialized version of the function, and override the base class' original.

EDIT: Think about a 2D shape base class. All 2D shapes have an area, but the formula is different for a circle, square, triangle, octogon, etc. A 2d shape base class doesn't know how to compute any specific area, so it declares a virtual function float shape_2d::area() in which the derived classes can override to implement their own area formula. (in this case float shape2D::area() would be declared as a pure virtual function, which simply means that any class deriving from shape_2d must override this function.)

Share this post


Link to post
Share on other sites
Quote:
Original post by stylin
Quote:
Original post by Avont29
so when you call one of the video functions like void init, it actually calls the other one in the subsystem baseclass?


No, it's the reverse. The derived class overrides the base class function, so when you call the base class function, it gets rerouted to the derived class' version. Making a function virtual means that it can be replaced by a function in a derived class. That means that the derived class can implement it's own specialized version of the function, and override the base class' original.




soif i had something like



class animal
{
public:
virtual void sleep();
}


class dog
{
public:
void sleep();
}


dog max;
max.sleep()








this would call dog::sleep right? because, the virtual keyword
//makes the animal:sleep function bound late.

Share this post


Link to post
Share on other sites
Quote:
Original post by Avont29
soif i had something like

*** Source Snippet Removed ***

this would call dog::sleep right? because, the virtual keyword

It would call dog::sleep because you're calling dog::sleep directly. You must actually derive dog from animal. Take a look at the following snippet, and see if you can dissect it:
#include <iostream>
using std::cout;
using std::endl;

class base {
public: virtual void virtual_function() { cout << "base::v_f()" << endl; return;}
};
class derived: public base {
public: virtual void virtual_function() { cout << "derived::v_f()" << endl; return;}
};
int main() {

// point to a base and a derived class with a base class pointer
base * pb1( new base ); // pb1 points to a base class
base * pb2( new derived ) ; // pb2 points to a derived class

// both of these call base::virtual_function()
pb1->virtual_function(); // this calls through a pointer
(*pb1).virtual_function(); // this calls through a reference

// both of these call derived::virtual_function(), since pb2
// points to a derived class that overrides virtual_function()
pb2->virtual_function(); // this calls through a pointer
(*pb2).virtual_function(); // this calls through a reference

delete pb1;
delete pb2;
return 0;

}

Share this post


Link to post
Share on other sites
Quote:
Original post by stylin
Quote:
Original post by Avont29
soif i had something like

*** Source Snippet Removed ***

this would call dog::sleep right? because, the virtual keyword

It would call dog::sleep because you're calling dog::sleep directly. You must actually derive dog from animal. Take a look at the following snippet, and see if you can dissect it:*** Source Snippet Removed ***


oh, yea, i forgot to make dog a subclass of animal with public, sorry

so am i right?

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!