C++程序員的負擔
C++的強大,毋庸置疑,嵌入式,操作系統,3D游戲引擎,無所不能,但是C++程序員的語言負擔真的很重,下面是一個正在嘗試開發的一個文件系統的頭文件,
#ifndef FILESYSTEM_H
#define FILESYSTEM_H
#include <string>
#include <vector>
#include <memory>
using namespace std;
namespace FS{
class FileSystemImpl;
class FileSystem{
public:
static shared_prt<FileSystem> init_file_system(const string& configFile);
static shared_ptr<FileSystem> mount(const string& mountPath);
~FileSystem();
public:
int create(const string& path, bool isDirectory=false);
int delete(const string& path);
int delete(const File& file);
vector<File> ls(const string& path);
vector<File> ls(const File& path);
private:
FileSystemImpl* impl;
};
class FileImpl;
class File{
friend class FileSystem;
public:
File(string& path);
~File();
public:
int open();
bool isDirectory();
bool isExist();
long length();
long available();
long position();
void seek(long position);
int read();
int read(char buffer[], int length);
int write(char buffer[], int length);
int flush();
int close();
string& path();
private:
FileImpl* impl;
};
enum FileSystemType
{
LOCAL,YADFS,HDFS
};
}
#endif
為什么選擇shared_ptr< FileSystem
>,不是簡單的FileSystem指針,能不能直接返回一個FileSystem引用?為什么不用shared_ptr<
FileSystemImpl >
impl,又直接用了FileSystemImpl指針,能不能用FileSystemImpl引用呢?參數用引用傳遞還是值傳遞,是傳一個指針呢還是傳
一個對象,要加const嗎?返回值是返回對象呢還是返回引用還是返回指針,要返回const嗎?string怎么考慮國際化的問題?Linux和
windows上wstring和string如何選?宏定義,友元類,前向聲明,異常,命名空間,虛函數,還有大量的類似typedef int
INT32之類的考慮,這個問題列表還能寫很長很長,你能想象你寫的每一個類都要經過這樣的思考過程嗎?C++
1x的引入雖然解決了一些問題,右值引用,移動語義,lambda,天啊,我知道他們都有用,但是真的又增加了很重的語法負擔,重到各路大大出來辯護要把
C++ 11當做一門全新的語言來學習,唉,為毛人人都喜歡auto,還不是因為減輕了一些聲明負擔啊。
來自:http://chatting8.com/?p=691
本文由用戶 jopen 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!