Complicated class function...?

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

Recommended Posts

Hello everyone
I've been stuck on one thing for couple hours and I can't get my head around this. My friend said it's possible and since I have no idea how you call this in c++, I don't know how to google this. hehe

What I'm trying to do is create a class, that is being inherited by nearly all classes in my game. That class will have empty run function, and I want each class that inherites that class, to have their own version of run function that runs totally different code then other classes.

here's a drawing (really bad one) to help it explain:

the lines represent inheritance

What I'm trying to achieve is command system. For example if i type in into command /setWeather Rain. It would run that function from weather and it would start raining in my game. Etc. Hope I explained this well hehe

Here's part of my code just in case:

[source lang="cpp"]class CommandC{
public:
CommandC();
~CommandC();
void *parent;
char* command;
void run(){};
};
[/source]
Thanks for taking your time and help Edited by iLoveGameProgramming

Share on other sites
What I'm trying to achieve is command system. For example if i type in into command /setWeather Rain. It would run that function from weather and it would start raining in my game.

Here are a couple of ideas:
1) Rename your objects to make your intent clearer. For instance, rename the interface CommandC to be something similar to "ICommandReceiver", and rename "run" to "receiveCommand".
2) Next, make "run"/"receiveCommand" take an array of strings -- the arguments ("apple", "orange") passed into the command from the string ("/sample apple orange").
3) Have a central object that parses a command string into command+arguments, and dispatching it to the appropriate object.

Share on other sites
Here's an example:
 #include <iostream> #include <vector> #include <string> #include <map> #include <sstream> #include <iterator> #include <algorithm> using namespace std; class ICommandReceiver { public: virtual ~ICommandReceiver() { } public: virtual bool receiveCommand( const vector<string>& args ) = 0; }; class SampleObject : public ICommandReceiver { public: bool receiveCommand( const vector<string>& args ) { cout << "SampleObject::receiveCommand, arguments are: {"; if ( args.size() ) { copy( args.begin(), args.end()-1, ostream_iterator<string>( cout, ", " ) ); cout << args.back(); } cout << "}\n"; return true; } }; class Console { public: bool executeCommand( string command ) { istringstream ss( command ); string obj; if ( !(ss >> obj) ) return false; map<string,ICommandReceiver*>::iterator itr = mRegisteredObjects.find( obj ); if ( itr == mRegisteredObjects.end() ) return false; // Not found vector<string> args = vector<string>( istream_iterator<string>(ss), istream_iterator<string>() ); return itr->second->receiveCommand( args ); } void registerObject( string name, ICommandReceiver* obj ){ mRegisteredObjects[name] = obj; } public: map<string, ICommandReceiver*> mRegisteredObjects; }; int main() { Console con; SampleObject obj; con.registerObject( "Test", &obj ); con.executeCommand( "Test apple orange banana" ); } 

Share on other sites
Thanks so much I'm trying to work out your sample code atm

Share on other sites
Just thought I'd be a little more explicit about the concept that you're trying to use (not that any of the information fastcall22 gave was wrong).

Google, or research information about virtual functions and pure virtual functions. That is what you're after.

Functions that are marked as being virtual in a class declaration are able to be overridden by derived classes using that same name for one of their functions. The idea is used in polymorphism.

 class Base { public: virtual void VAnnounce(); }; class Derived : public Base { public: virtual void VAnnounce(); } 

In the above example Derived is free (but does not have too) to define it's own function that will be used when an instance is accessed through a pointer to Base.

 Base *pBase = new Derived(); pBase->VAnnounce(); 

The extension, pure virtual functions, indicate that there is no definition in the base class and that any derived types must write there own definition.

 class IBase { public: virtual void VAnnounce() = 0; }; class Derived : public Base { public: virtual void VAnnounce(); } 

Derived must contain a definition for VAnnounce.

Functions that contain pure virtual function become interfaces and it isn't possible to declare objects of that type.

Hopefully that explains what you're after in slightly more detail about the general topic. Edited by BinaryPhysics

Share on other sites
Thanks, I actually got it working in very similar way

• 10
• 11
• 9
• 11
• 9