外围函数不能访问局部类的公有静态成员、类型名、枚举 值(类型名是一个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;
}

 

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

posted @ 2012-04-04 20:04  carbs  阅读(444)  评论(0)    收藏  举报