Re: [问题] 规划、分割 class 与 header 架构

楼主: damody (天亮damody)   2014-05-11 23:18:08
※ 引述《cory8249 (Cory)》之铭言:
: 问题(Question):
: 请问如何正确规划、分割 class 与 header 架构 ?
: 小弟目前写一个 CPU 模拟器
: 开了以下几个 class :
: Core, Instruction, Memory, Regsiter, ALU, Controller
: 不晓得怎么档案架构分割比较好
: 想请教板上各位高手
: 首先 Core 是最大的一个 class 内部包含 Instruction , Memory ... 等等
: 而 ALU , Controller 又需要存取 Instruction , Memory, Register
: 1.
: 那这样在 Core.h 的 class Core 宣告中 要用 forward declaration 吗 ?
: 例如:
: class ALU;
: class Controller;
: class Memory;
: class ...
: class Core{
: public:
: ....
: private:
: Memory *imem, *dmem; // OK
: ALU alu; // compile error : field alu has incomplete type
: Controller controller; // compile error : field ... has incomplete type
: };
: forward delclaration 似乎只能用 pointer 的方式 ?

: 所以在 Core 里面包含的其他 class 看似都要用 pointer 囉 ?
对,这我也有想过,不过在c++中一定要这样用
不然会 include 循环就编不过
: 2.
: 因为有很多讯号、指令等等 我想开 enum 去写
: 但是 enum 好像只能宣告定义一次而已
: 假设我在 Instruction.h 中定义了 enum InstType { R_TYPE, I_TYPE, J_TYPE };
: 但是 ALU 、 Controller 中也需要用到这个 enum
: 所以 ALU 、 Controller 要 #include "Instruction.h" ?
对,所以你可以定一个 .h 档,专门去定义 enum 或 const int 常数
: 3.
: 因为 ALU , Controller 会互相存取资料、 call function 等等
: Header include 要怎么写比较好 ?
: 可以互设为 friend class 吗 ?
: class ALU;
: class Controller{
: public:
: friend class ALU;
: private:
: ...
: }
: 像是这样 ?
: 问题有点多 不好意思 麻烦各位了 感谢
用 public GetXXX() 函数的方式拿,
friend 没有 public private 的分别
他永远是你的好朋友 :)
不然的话你用有MVC的感觉
ALU 分成
ALU_Model ALU_View ALU_Control
放rawdata 放资料转型或处理 放控制指令
我的感觉是你可以再参考一下
设计模式里的 singleton 来拿资料
Iterator 跟 Command 来处理指令
Strategy 来产生不同的 memory model
然后抱着一定会打掉重练的心情去写程式吧~
才不会得失心太重,第一次写总是会有一堆问题的,
不太可能一步到位的。
作者: cory8249 (Cory)   2014-05-11 23:31:00
感谢!! 事实上我已经砍一大半重练了 QQ

Links booklink

Contact Us: admin [ a t ] ucptt.com