I have a class called variableMap which keeps track of variable values in a calculator:
//parser.h
namespace parser
{
class VariableMap
{
private:
std::unordered_map<std::string, std::string> variables{};
std::vector<Token> replaceVars(std::vector<Token> tokens) const;
public:
double parseExp(std::vector<Token> tokens);
};
}
From posts like this I have learned that it is good practice to keep functions that does not handle data members of a class as separate functions within a namespace. So I implemented a couple of functions that does not directly deal with the variables
map inside the namespace parser:
//parser.cpp
namespace parser
{
void displayTokens(const std::vector<Token>& tokens)
{
// does stuff
}
namespace
{
std::string sumTokens(const std::vector<Token>& tokens)
{
// does stuff
}
}
}
The problem is that functions like sumTokens()
are dependent on the non-static method replaceVars()
. Meaning I would somehow have to give sumTokens()
access to the objects of VariableMap. Is it not just better to make sumTokens() a static method of the class in scenarios like these?