S
S
Sazoks2019-08-07 05:46:43
OOP
Sazoks, 2019-08-07 05:46:43

Is using static justified in my case?

I have some class. In this class, I have the functionality of processing user commands. Let 's say our "bash" ..
And now I have a question. In the class I have a private std::map, this map consists of a pair of .
There is also a CommandListener method that accepts a string from the user. Further, this method searches for a key match with the user's string and, if found, executes the value, that is, the method.
Example code:
map

std::map<std::string, void(*)()> Functions = {
    {"SomeF1", SomeF1},
    {"SomeF2", SomeF2},
    {"SomeF3", SomeF3},
    {"SomeF4", SomeF4},
                // ...
  };

Handler:
void Client::CommandListener(std::string Command)
{
  std::map<std::string, void(*)()>::iterator it = Functions.find(Command);
  if (it == Functions.end()) std::cout << "This command does not exist\n";
  else it->second();
}

But! The problem is that map refuses to accept NON-STATIC methods! Those. SomeF1 (and others) I had to declare static, and that's the only way it worked.
I did it purely by accident, and now my solution .. Well, it seems to me a crutch =(
Tell me, what is the best thing to do in my case?
Thank you!

Answer the question

In order to leave comments, you need to log in

1 answer(s)
J
jcmvbkbc, 2019-08-07
@Sazoks

Tell me what is the best way to proceed in my case?

Learn how static class methods differ from regular methods.
Find out that in addition to function pointers, there are pointers to member functions.
Understand which of these is better to use in your case.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question