首页 > 学院 > 开发设计 > 正文

嵌套类、局部类

2019-11-06 09:10:27
字体:
来源:转载
供稿:网友

嵌套类(内部类):在类内定义一个类

class Test

{

public:

    class Inner

{

    public:

        void func();

        int num;

};

PRivate:

};

类外实现函数:

void Test::Inner::func(){}

类外调用类内的类的成员:

Test t;t.Inner::num = 1;//错误,不能通过外部对象访问内部类的成员

Test::Inner n;n.num = 1;

 

//示例:

#include<iostream>  usingnamespace std;   classTest { private: intx_; inty_; intz_; public: voidinitXYZ(int x, int y, int z) { this->x_= x; this->y_= y; this->z_= z; } }; intmain() { Test*t1 = new Test(); t1->initXYZ(1,2, 4);   return0; }

局部类:

栈空间定义的类à局部类不能有静态成员;成员函数必须在类内实现

 


发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表