DoubleLi

qq: 517712484 wx: ldbgliet

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::
  4737 随笔 :: 2 文章 :: 542 评论 :: 1615万 阅读
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

C++嵌套类

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

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

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

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

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

#include "stdafx.h"
#include
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::mothersClass constructed "<<endl;
        }
        int  funT();
        int getmothersT();
        //int getMotherClassAA()
        //{
        //    return MotherClass::aa;
        //}
        //int getMotherClassBB()
        //{
        //    MotherClass::bb=1234;
        //    return MotherClass::bb;

        //}
    protected:
    private:
        int mothersT;
    };
protected:

public:

   
  

 


private:
    int a;
};//

//static int MotherClass::aa =100;

int MotherClass::fun()
{
   mothersClass mothersClassT(1);
   return mothersClassT.getmothersT();

}
int MotherClass::getA()
{
    //a= mothersClass::getmothersT();//error
  return a;
}
int MotherClass::mothersClass::getmothersT()
{
    return mothersT;

}
int MotherClass::mothersClass::funT()
{
    MotherClass MotherClassT(2);

    return MotherClassT.getA();
}

int _tmain(int argc, _TCHAR* argv[])
{

    MotherClass myClass(3);
    MotherClass::mothersClass myClassT(4);
    MotherClass::mothersClass myClassTT(5);
    int a= myClass.getA();
    cout<<"MotherClass::getA()="<<a<<endl;
    cout<<"MotherClass::fun()="<<myClass.fun()<<endl;
    a= myClassT.getmothersT();
    cout<<"MotherClass::mothersClass::getmothersT()="<<a<<endl;
    a=myClassT.funT();
    cout<<"MotherClass::mothersClass::funT()="<<a<<endl;
   
    //cout<<"MotherClass::mothersClass.getMotherClassAA()="<<MotherClass::mothersClass.getMotherClassAA()<<endl;
    cin.get();

 return 0;
}

 

下面内容是从网上贴来的。

下面内容是从网上贴来的。

 

 

 

C++嵌套类

 嵌套类的访问问题:

记得白凤煮的C++中有一句这样的话:C++嵌套类只是语法上的嵌套。然而在实践过程中,却并非如此。

Ex:

 class A

  {

 public: 

     static int a;

     class A1

      {

         void output()

          {

           cout<<a<<endl; //instead of A::a;

         }

     };

  

 };

 int A::a;

可见,类 A1 嵌入A后访问A的静态变量不写外围域没有任何问题,从编译的角度看,此时位于A::的作用域内,在符号表中是可以找到a(注意,a必须为static)。这一点,与分开写的两个类明显不同

 

还有一个特殊之处,见如下代码:

 

 Ex:

 

 class A

  {

 private:

     int a;

     class A1

      {

         void output(A aObject)

          {

           cout<<aObject.a<<endl; //instead of A::a;

         }

     };

  

 };

这段代码在VC中不能编译通过,但在DEVC++是可以的,也就是不同的编译对于嵌套类是否能访问外围类的私有成员的定义是不一致的。

嵌套类的不同作用域同名问题:

 class A

  {

 public: 

     static int a;

     class A1

      {

         static int a;

         int    void output()

          {

           cout<<a<<endl; //instead of A::a;

         }

     };

  

 };

 int A::a=3;

 int A::A1::a=4;

输出内部的a没有问题,如果要访问外部的,使用A::a指明作用域就可以,而且在嵌套类中,可以定义静态的成员。

posted on   DoubleLi  阅读(1247)  评论(0编辑  收藏  举报
编辑推荐:
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 单线程的Redis速度为什么快?
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 展开说说关于C#中ORM框架的用法!
历史上的今天:
2012-06-06 关于C++中函数指针的使用
点击右上角即可分享
微信分享提示