Reputation: 13
I want to write a utility in my program which can convert a string to int. I know that I could use atoi or strtol for it but I need some error handling on it. Which is the better way to do it? Should I create a simple global function, maybe only in a specific namespace, or create a class that have a member which can do it for me?
For eg.:
namespace utility{
int ConvertStrToInt(std::string* str, int& convertednum)
{
//do the conversion and error handling
return convertednum;
}
}
or
class Utility{
public:
static int ConvertStrToInt(std::string* str, int& convertednum)
{//do the conversion and error handling here}
}
Sorry if the question sounds a bit silly but I am in a team with 2 other guy and we think about this very differently. 1 says the class is for everything and make classes for everything, and I think that for such a simple problem a simple function is a good solution.
So which is the more efficient? When should I use a simple function and when is the point from where class is the good solution?
Thx for everyone!
Upvotes: 1
Views: 276
Reputation: 153909
I would normally just use a function. Putting it into a class is just noise (or pseudo-OO, since the class doesn't have any real behavior on its own).
There is one exception: functional template arguments to the STL are
generally more efficient if you use a class with an operator()()
,
rather than a function. (If you use a functional object, the actual
function being called is a compile time constant, and can easily be
inlined. If you use a function, the template argument is the type of
the function, not the function itself, and inlining is less likely.)
Even in this case, however, I'd start with the function, and add the
functional object type if needed.
Upvotes: 0
Reputation: 227400
If it is useful to have state in your conversion, use a class. Preferably, a functor so you can pass an instance around as a callable entity. If there is no state, then use a function.
As an aside, your signature and return type should probably look like this:
int ConvertStrToInt(const std::string& str);
Edit concerning this particular example, the C++ standard library provides this functionality already, so you don't need to re-implement it.
Upvotes: 2
Reputation: 1891
The class Utility
as you have written it down above somehow contradicts the idea behind object oriented programming, as the method neither uses nor depends on any members of the class. Classes should rather be objects that have certain properties and methods on these properties.
Upvotes: 5
Reputation: 59811
Neither. Go for a function object (sometimes called a Functor).
struct str_to_int {
int operator()(const std::string& s) const { return 23; }
};
Why? This gives you the ability to add state if you need it. It works with all standard algorithm facilities and every modern C++ library. You can make it a template function without your users every noticing it.
Now you can do things like:
std::vector<std::string> strings;
std::vector<int> integers;
std::transform(begin(strings), end(strings),
std::back_inserter(integers), str_to_int());
Or someday turn your definition into:
struct str_to_int {
int operator()(const std::string& s) const { return 23; }
// ha
int operator()(const std::wstring& s) const { return 42; }
// haha!
int operator()(const char* x) const { return 42; }
};
and the above code will continue to work without a problem. This wont be the case for a free function.
Random Remark: Why would you pass a pointer to a string
for
something like that?
Upvotes: 0
Reputation: 258588
namespace
is the usual way to go about this.
The function in the class should be declared static
anyway, and having a class just so you can group functions together isn't good practice.
Upvotes: 1