嵌套类(内部类):在类内定义一个类
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; }局部类:
栈空间定义的类à局部类不能有静态成员;成员函数必须在类内实现
新闻热点
疑难解答