C++ 嵌套类使用

2023-11-19

C++嵌套类

1   嵌套类的名字只在外围类可见。

2   类的私有成员只有类的成员和友元可以访问,因此外围类不可以访问嵌套类的私有成员。嵌套类可以访问外围类的成员(通过对象、指针或者引用)。

3   一个好的嵌套类设计:嵌套类应该设成私有。嵌套类的成员和方法可以设为 public

4   嵌套类可以直接访问外围类的静态成员、类型名( typedef )、枚举值。

// qiantaolei.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <iostream>
using namespace std;
class MotherClass
{
public:
    MotherClass(int b)
    {
        a=b;
        cout<<"MotherClass constructed "<<endl;
    }
   int  fun();
 
   int getA();
public:
   
    class mothersClass
    {
    public:
        mothersClass(int b)
        {
            mothersT =b;
            cout<<"MotherClass::moth

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

C++ 嵌套类使用 的相关文章