Re: [問題] 為什麼作業系統都用C寫? 而不用C++呢?
目前還在學習怎麼寫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)
討論串 (同標題文章)
本文引述了以下文章的的內容:
完整討論串 (本文為第 18 之 37 篇):