Reputation: 127
I have a class with non static functions. Because of the architecture of my program I think it will be better to use static functions since the class is just an utility. In some cases I just need one function of the class so I see unnecessary to create the object. Basically I have this:
class StaticCall
{
public:
StaticCall(){}
static int call_1()
{
std::cout << "In call 1" << std::endl;
call_2();
return 0;
}
static int call_2();
{
std::cout << "In call 2" << std::endl;
return 0;
}
};
int main( int argv, char** argc )
{
std::cout << "Calling 1" << std::endl;
StaticCall::call_1();
std::cout << std::endl;
std::cout << "Calling 2" << std::endl;
StaticCall::call_2();
return 0;
}
It works fine, but I was wondering if there could be any problems with this method. I can achieve the same by using a namespace as other posts already say. But since I already have the class I want to do it with static functions.
Upvotes: 1
Views: 153
Reputation: 4951
what you are doing here is to use a class to encapsulate two methods - the same as a namespace would do. You are essentially replacing the use of a namespace with a class.
Does it work?? Yes, it does!
Is it ok to do this? Well, from a code maintenance/readability point of view, not so much, because:
If all you want is encapsulation, or a way to not pollute the global namespace - that's why you have namespaces.
The code is cleaner, more readable and a lot less complicated when you properly use the language features c++ offers.
Upvotes: 2