嵌套类的使用

来源:互联网 发布:aws ubuntu root密码 编辑:程序博客网 时间:2024/05/16 12:01

<span style="font-family: Arial, Helvetica, sans-serif; background-color: rgb(255, 255, 255);">嵌套类规范  </span>

class COutClass{public:COutClass();~COutClass();class CInnerClass{public:CInnerClass();~CInnerClass();private:};private:};

CoutClass  外围类

CInnerClass 嵌套类

不动守则  不论是嵌套类还是外围类  都没有特殊访问的权限。遵循 public,private 访问原则


使用嵌套类,很直观的好处是,外围类可以使用嵌套类做一些事情(加工数据等等),但是嵌套类不会暴露给除了外围类的其他类,有点像外围类独占。。。

嵌套类没有特殊权限,但是可以直接访问外围类的 公有 静态成员,枚举,类型名(typedef的名字),这是作为在外围类域内部所获得的权限


其他特性,比如友元这种就不说了。


贴一段Ogre里的代码

    class _OgreExport Node : public NodeAlloc    {    public:        /** Enumeration denoting the spaces which a transform can be relative to.        */        enum TransformSpace        {            /// Transform is relative to the local space            TS_LOCAL,            /// Transform is relative to the space of the parent node            TS_PARENT,            /// Transform is relative to world space            TS_WORLD        };        typedef HashMap<String, Node*> ChildNodeMap;        typedef MapIterator<ChildNodeMap> ChildNodeIterator;typedef ConstMapIterator<ChildNodeMap> ConstChildNodeIterator;/** Listener which gets called back on Node events.*/class _OgreExport Listener{public:Listener() {}virtual ~Listener() {}/** Called when a node gets updated.@remarksNote that this happens when the node's derived update happens,not every time a method altering it's state occurs. There may be several state-changing calls but only one of these calls, when the node graph is fully updated.*/virtual void nodeUpdated(const Node*) {}/** Node is being destroyed */virtual void nodeDestroyed(const Node*) {}/** Node has been attached to a parent */virtual void nodeAttached(const Node*) {}/** Node has been detached from a parent */virtual void nodeDetached(const Node*) {}};/** Inner class for displaying debug renderable for Node. */class DebugRenderable : public Renderable, public NodeAlloc{protected:Node* mParent;MeshPtr mMeshPtr;MaterialPtr mMat;Real mScaling;public:DebugRenderable(Node* parent);~DebugRenderable();const MaterialPtr& getMaterial(void) const;void getRenderOperation(RenderOperation& op);void getWorldTransforms(Matrix4* xform) const;Real getSquaredViewDepth(const Camera* cam) const;const LightList& getLights(void) const;void setScaling(Real s) { mScaling = s; }};


只贴了Node类的前半部分

class _OgreExport Node : public NodeAlloc
Node继承一个内存分配基类
<pre name="code" class="cpp">_OgreExport  使用外部动态链接库
下来是public的一些枚举,typedef的数据结构
这些都是内部类可以使用的。
后面是重点,两个嵌套类,一个用来监听Node事件的,一个用来测试Node
两个内部类都是单独的任务,多一层封装
<pre name="code" class="cpp">class DebugRenderable : public Renderable, public NodeAlloc
看这个类的声明,也有继承NodeAlloc

<span style="font-family: Arial, Helvetica, sans-serif;">说明嵌套类没有什么特别的地方,只是写在一个类内部而已,所在域比较特殊</span>
<span style="font-family: Arial, Helvetica, sans-serif;"></span>
<span style="font-family: Arial, Helvetica, sans-serif;">在这个类</span>
<span style="font-family: Arial, Helvetica, sans-serif;">class _OgreExport RibbonTrail : public BillboardChain, public Node::Listener</span>
<span style="font-family: Arial, Helvetica, sans-serif;">带追踪中,</span><span style="font-family: Arial, Helvetica, sans-serif;">Listener作为一个独立的部分被拿过来继承</span>




0 0
原创粉丝点击