C++程序员的负担

jopen 9年前

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