1 c++
我想在我的程序中编写一个实用程序,它可以将字符串转换为int.我知道我可以使用atoi或strtol,但我需要对它进行一些错误处理.哪种方式更好?我应该创建一个简单的全局函数,可能只在特定的命名空间中创建,还是创建一个具有可以为我执行此操作的成员的类?
例如:
namespace utility{
int ConvertStrToInt(std::string* str, int& convertednum)
{
//do the conversion and error handling
return convertednum;
}
}
Run Code Online (Sandbox Code Playgroud)
要么
class Utility{
public:
static int ConvertStrToInt(std::string* str, int& convertednum)
{//do the conversion and error handling here}
}
Run Code Online (Sandbox Code Playgroud)
对不起,如果这个问题听起来有点傻,但我和另外两个人在一个团队中,我们对此的看法截然不同.1表示该类适用于所有内容并为所有内容创建类,我认为对于这样一个简单的问题,一个简单的函数是一个很好的解决方案.
哪个更有效率?我什么时候应该使用一个简单的函数?什么时候从哪个类开始是一个好的解决方案?
感谢每个人!
| 归档时间: |
|
| 查看次数: |
245 次 |
| 最近记录: |