Sign in to follow this  
iLoveGameProgramming

Complicated class function...?

Recommended Posts

Hello everyone [img]http://public.gamedev.net//public/style_emoticons/default/smile.png[/img]
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


[img]http://dl.dropbox.com/u/80015266/run.png[/img]


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 [img]http://public.gamedev.net//public/style_emoticons/default/smile.png[/img] Edited by iLoveGameProgramming

Share this post


Link to post
Share on other sites
fastcall22    10846
[quote name='iLoveGameProgramming' timestamp='1339788499' post='4949639']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.[/quote]

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 this post


Link to post
Share on other sites
fastcall22    10846
Here's an example:
[code]
#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" );
}
[/code]

Share this post


Link to post
Share on other sites
Alex Melbourne    294
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 [b]virtual functions[/b] and [b]pure virtual functions[/b]. 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.

[CODE]
class Base
{
public:
virtual void VAnnounce();
};
class Derived : public Base
{
public:
virtual void VAnnounce();
}
[/CODE]

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.

[CODE]
Base *pBase = new Derived();
pBase->VAnnounce();
[/CODE]

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

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

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 this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this