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

楼主: cory8249 (Cory)   2014-05-11 21:24:35
问题(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 囉 ?
2.
因为有很多讯号、指令等等 我想开 enum 去写
但是 enum 好像只能宣告定义一次而已
假设我在 Instruction.h 中定义了 enum InstType { R_TYPE, I_TYPE, J_TYPE };
但是 ALU 、 Controller 中也需要用到这个 enum
所以 ALU 、 Controller 要 #include "Instruction.h" ?
3.
因为 ALU , Controller 会互相存取资料、 call function 等等
Header include 要怎么写比较好 ?
可以互设为 friend class 吗 ?
class ALU;
class Controller{
public:
friend class ALU;
private:
...
}
像是这样 ?
问题有点多 不好意思 麻烦各位了 感谢
楼主: cory8249 (Cory)   2014-05-11 21:35:00
刚刚看到一篇不错的: http://goo.gl/eKQkGh
作者: damody (天亮damody)   2014-05-11 23:05:00
你的问题不适合断行
作者: plover (喜欢你 ( ̄▽ ̄#)﹏﹏)   2014-05-14 10:27:00
http://ppt.cc/kdCs 或许可以参考
作者: damody (天亮damody)   2014-05-14 12:12:00

Links booklink

Contact Us: admin [ a t ] ucptt.com