作者karcher (Grothendieck )
看板C_and_CPP
标题Re: [问题] 为什麽作业系统都用C写? 而不用C++呢?
时间Sat Mar 7 21:15:39 2009
目前还在学习怎麽写C/C++
还不习惯用static 的方式将remove写在各个子.c file
小弟将M大的方法转用C++表现...
1. in header file
enum{
LOCAL_FILE_SYSTEM =1,
REMOTE_FILE_SYSTEM ,
..
..
};
class FS_interface{
public:
virtual int (remove)(const char * path);
// ..
// ..
};
2. FileSystem.cpp
#include ......
template <unsigned int SystemType>
class FileSystem : public FS_interface {
};
template <>
class FileSystem<LOCAL_FILE_SYSTEM>:public FS_interface{
char * strCurrentDirectory;
// ..
pubilc:
virtual int (remove)(const char * path){
..
..
return 0;
}
};
template <>
class FileSystem<REMOTE_FILE_SYSTEM>:public FS_interface{
char * remotePath;
// ..
pubilc:
virtual int (remove)(const char * path){
..
..
return 0;
}
};
FS_interface* pFS = new FileSystem<LOCAL_FILE_SYSTEM>;
pFS->remove("Nothing");
※ 引述《meltice (三亿两千万大散户)》之铭言:
: 回应littleshan
: 1.
REMOTE_FILE_SYSTEM: in header file
: struct FS
: {
: int (*remove)(const char *path);
: };
: #define FS(type) type##_fs
: in local_fs.c
: static int remove(const char *path){...}
: struct FS local_fs;
: local_fs.remove = remove;
: in remote_fs.c
: static int remove(const char *path){...}
: struct FS remote_fs;
: remote_fs.remove = remove;
: Then you can call anywhere like,
: struct FS *fsp = &FS(remote);
: fsp->remove("filename");
: 2.
: lock(r1);
: if(...)
: goto exit1;
: lock(r2)
: if(...)
: goto exit2;
: ...
: exit2:
: unlock(r2);
: exit1:
: unlock(r1);
--
※ 发信站: 批踢踢实业坊(ptt.cc)
◆ From: 220.137.135.99
※ 编辑: karcher 来自: 220.137.135.99 (03/07 21:17)
※ 编辑: karcher 来自: 220.137.135.99 (03/07 21:18)
※ 编辑: karcher 来自: 220.137.135.99 (03/07 21:20)
※ 编辑: karcher 来自: 220.137.135.99 (03/07 21:24)
※ 编辑: karcher 来自: 220.137.135.99 (03/07 21:26)
※ 编辑: karcher 来自: 220.137.135.99 (03/07 21:33)
※ 编辑: karcher 来自: 220.137.135.99 (03/07 21:34)
※ 编辑: karcher 来自: 220.137.135.99 (03/07 21:41)
※ 编辑: karcher 来自: 220.137.135.99 (03/07 21:45)