zoukankan      html  css  js  c++  java
  • 外围函数不能访问局部类的公有静态成员、类型名、枚举 值(类型名是一个typedef名字,枚举类型名、或一个类名)

    #include <iostream>
    using namespace std;
    void  foo()
    {   
        class  Bar
        {
        public:
            enum test
            {one,two};
            typedef int INTE;
            Bar(int arg=0):m_iVal(arg)
            {
            }
            int get() const
            {
                return m_iVal;
            }
        private:
            int m_iVal;
        };

    Bar obj(100);
     //INTE a; // ERROR
     //cout << one << endl; // ERROR
     //test en; // ERROR
        cout << obj.get() << endl;
    }

    int main()
    {
        foo();

        return 0;
    }

    局部类被完全限定在了函数体这个名字空间中,函数外无法使用这个类定义对象!
    #include <iostream>
    using namespace std;
    void  foo()
    {   
        class  Bar
        {
        public:
            enum test
            {one,two};
            typedef int INTE;
            Bar(int arg=0):m_iVal(arg)
            {
            }
            int get() const
            {
                return m_iVal;
            }
        private:
            int m_iVal;
        };
    }

    int main()
    {
        Bar obj;//Error

        return 0;
    }

    本质上讲局部类是就是函数的内部“类型成员”,只能在函数体内使用这个类型定义对象,而不能在函数外面使用这个类型

  • 相关阅读:
    Android 自定义ProgressDialog
    Android 设置TextView字体颜色
    Android 隐藏EditText的焦点
    Android TextView点击效果
    区间问题 codeforces 422c+hiho区间求差问
    hdu 5651 重复全排列+逆元
    hdu 1576
    C Looooops
    扩展欧几里得模板
    hdu 6025(女生赛)
  • 原文地址:https://www.cnblogs.com/carbs/p/2432280.html
Copyright © 2011-2022 走看看