Jim*_* Lu 9 c++ encapsulation namespaces class
所以,假设我有一些函数来处理文件的打开/关闭.
是否更好地创建一个具有静态声明的所有这些函数的类,或者只是将"public"函数放在命名空间"file"的头文件中,并将其余的"实现细节"放在.cc文件中?
以下是代码示例.
命名空间一有点长,因为我想让它尽可能清晰.
谢谢!!
类实现
标题:
#ifndef FILE_H
#define FILE_H
#include <iostream>
#include <fstream>
include "common.h"
enum Errorcode {
FILE_CANNOT_OPEN,
FILE_CANNOT_CLOSE
};
class file {
public:
static common::Lines toLines(std::string filename);
private:
static void err(Errorcode e, std::string msg);
static void toLines(std::ifstream &ifs, common::Lines &lines);
};
#endif
Run Code Online (Sandbox Code Playgroud)
.cc文件:
/*just the implementation details of above class.*/
Run Code Online (Sandbox Code Playgroud)
命名空间实现
标题:
#ifndef FILE_H
#define FILE_H
#include <iostream>
#include <fstream>
#include "common.h"
namespace file {
common::Lines toLines(std::string filename);
}
#endif
Run Code Online (Sandbox Code Playgroud)
.cc文件:
namespace file {
enum Errorcode {
FILE_CANNOT_OPEN,
FILE_CANNOT_CLOSE
};
void err(Errorcode e, std::string msg);
void toLines(std::ifstream& ifs, common::Lines &lines);
common::Lines toLines(std::string filename)
{
std::vector<std::string> lines;
try {
std::ifstream ifs(filename.c_str());
if (ifs.fail()) throw FILE_CANNOT_OPEN;
toLines(ifs, lines);
ifs.close();
if (ifs.fail()) throw FILE_CANNOT_CLOSE;
}
catch (Errorcode e) {
err(e, filename);
}
return lines;
}
void err(Errorcode e, std::string msg)
{
switch (e) {
default:
std::cerr << "Unknown error.\n";
break;
case FILE_CANNOT_OPEN:
std::cerr << "file \"" << msg
<< "\" could not be opened.\n";
break;
case FILE_CANNOT_CLOSE:
std::cerr << "file \"" << msg
<< "\" could not be closed.\n";
break;
}
std::exit(-1);
}
void toLines(std::ifstream& ifs, common::Lines &lines)
{
std::string line;
while(std::getline(ifs, line)) {
lines.push_back(line);
}
ifs.clear(); // clear error bit set by getline()
}
}
Run Code Online (Sandbox Code Playgroud)
Ker*_* SB 11
从表面上看,静态类函数和命名空间函数几乎完全相同,实际上,在命名空间支持变得普遍之前的早期使用了类.
如今,你应该做大多数表达你的程序的逻辑结构(即心理模型).如果要对相关函数进行分组,则它是命名空间.
但是,简而言之,技术差异在于命名空间参与依赖于参数的查找(ADL),而类成员函数则不参与,但类可以转换为模板和专用.如果这些语义差异对您来说很重要,那么这种考虑可能会帮助您做出正确的选择.
有一个简单的问题涵盖了大多数情况:如果你把它变成一个类,那个类的实例是否有意义并完成一些有用的东西?
如果实例有用,那么您需要一个类.否则,命名空间可能是更好的选择.