Jump to content

  • Log In with Google      Sign In   
  • Create Account

We're offering banner ads on our site from just $5!

1. Details HERE. 2. GDNet+ Subscriptions HERE. 3. Ad upload HERE.


Complicated class function...?


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
5 replies to this topic

#1 Konrad Jablonski   Members   -  Reputation: 483

Like
0Likes
Like

Posted 15 June 2012 - 01:28 PM

Hello everyone Posted Image
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


Posted Image


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 Posted Image

Edited by iLoveGameProgramming, 15 June 2012 - 01:29 PM.


Sponsor:

#2 fastcall22   Crossbones+   -  Reputation: 4454

Like
3Likes
Like

Posted 15 June 2012 - 01:41 PM

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.
c3RhdGljIGNoYXIgeW91cl9tb21bMVVMTCA8PCA2NF07CnNwcmludGYoeW91cl9tb20sICJpcyBmYXQiKTs=

#3 fastcall22   Crossbones+   -  Reputation: 4454

Like
3Likes
Like

Posted 15 June 2012 - 01:51 PM

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" );
}

c3RhdGljIGNoYXIgeW91cl9tb21bMVVMTCA8PCA2NF07CnNwcmludGYoeW91cl9tb20sICJpcyBmYXQiKTs=

#4 Konrad Jablonski   Members   -  Reputation: 483

Like
0Likes
Like

Posted 15 June 2012 - 01:58 PM

Thanks so much :) I'm trying to work out your sample code atm :P

#5 BinaryPhysics   Members   -  Reputation: 294

Like
1Likes
Like

Posted 15 June 2012 - 04:40 PM

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, 15 June 2012 - 04:46 PM.


#6 Konrad Jablonski   Members   -  Reputation: 483

Like
0Likes
Like

Posted 16 June 2012 - 11:22 AM

Thanks, I actually got it working in very similar way :)




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