How to store and call specific function instances

I am trying to program a developer console for a game I'm working on. To test if it worked, I created a test that is a static function for a command. However, when I tried to use a function in a specific instance of a class that was non-static, my compiler threw errors.

This is the header file for my command creator
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
class Command
{
	friend class Console;
	friend class ConsoleRun;
public:
	std::string getCvar(std::string name);

	static std::unordered_map<std::string, Command> & create_map()
	{
		static std::unordered_map<std::string, Command> mymap;
		// init your map here
		return mymap;
	}

	static std::unordered_map<std::string, Command> & get_map()
	{
		static std::unordered_map<std::string, Command> & mymap = create_map(); // this is only called once!
		return mymap; // subsequent calls just return the single instance.
	}

	static std::unordered_map<std::string, CVar> & create_vap()
	{
		static std::unordered_map<std::string, CVar> myvap;
		// init your map here
		return myvap;
	}

	static std::unordered_map<std::string, CVar> & get_vap()
	{
		static std::unordered_map<std::string, CVar> & myvap = create_vap(); // this is only called once!
		return myvap; // subsequent calls just return the single instance.
	}


	static void create(int(&handler) (std::vector<std::string> args), const CommandInfo& infoObject);
	static void createCVar(const CVar& varObject);
private:
	int(*m_handler) (std::vector<std::string> args);
	CommandInfo cmdInfo;
	static int currentIndex;
};


This seemed to work
 
Command::create(Console::help, CommandInfo("help", "Lists all commands.", "help OR help <command||cvar> OR help cvar"));


Yet when where it says 'Console::help' is non-static, it fails. I even tried this->help.

I am running in visual studio 2017 on windows.
Sooo is Console::help static or not? The dereference operator (->) has nothing to do with whether a function is static or not.
Console::help is static. I then attempted to remove the static keyword from its definition as a test as there is another part of my code that absolutely can't be static.
You can't call a non static function from within a static function. Not sure if what I said helps.
Last edited on
Something along these lines, perhaps:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#include <iostream>
#include <functional>
#include <string>
#include <vector>
#include <map>

struct command_processor
{
    // http://en.cppreference.com/w/cpp/utility/functional/function
    using command = std::function< void( std::vector<std::string> args ) > ;

    std::map< std::string, command > commands ;

    void execute( const std::string& cmd_name, std::vector<std::string> args = {} ) const
    {
        std::cout << "\nexecute command '" << cmd_name << "' => " ;
        const auto iter = commands.find(cmd_name) ;
        if( iter != commands.end() ) iter->second( std::move(args) ) ;
    }

    void list() const
    {
        std::cout << "list of commands\n------------\n" ;
        for( const auto& pair : commands ) std::cout << "    " << pair.first << '\n' ;
    }
};

int main()
{
    struct A
    {
        void do_it( std::vector<std::string> args ) const
        {
            std::cout << "main::A::do_it called with args: " ;
            for( const auto& a : args ) std::cout << a << ' ' ;
            std::cout << '\n' ;
        }
    };

    A a ;

    command_processor cp
    {{
        // http://en.cppreference.com/w/cpp/utility/functional/bind
        { "do_it", std::bind( &A::do_it, std::addressof(a), std::placeholders::_1 ) },
        { "say_hello" , [] ( auto ) { std::cout << "hello from a closure object\n" ; } }
    }};

    cp.commands.emplace( "another_command" , [] ( auto ) { std::cout << "another command\n" ; } ) ;

    cp.list() ;

    cp.execute( "do_it", { "one", "two", "three" } ) ;
    cp.execute( "say_hello" ) ;
}

http://coliru.stacked-crooked.com/a/7e33a391dc92b409
Thanks, that seems to work.
Topic archived. No new replies allowed.