1.局部类不能定义静态成员函数
#include <iostream>
using namespace std;
int main(void)
{
class test
{
static int a;
};
cout << sizeof(test) << endl;
}
编译报错如下:
a.cpp: In function ‘int main()’:
a.cpp:10:20: error: local class ‘class main()::test’ shall not have static data member ‘int main()::test::a’ [-fpermissive]
static int a;
^
2.局部类允许访问外围作用域中的枚举和静态变量
#include <iostream>
using namespace std;
enum RR
{
FD = 100
};
static int a = 10;
void f(void)
{
enum {ABC = 400};
static int b = 20;
class A
{
int print ()
{
cout << FD << endl;
cout << a << endl;
cout << ABC << endl;
cout << b << endl;
}
};
}
int main(void)
{
f();
return 0;
}
3.外围函数对局部类的保护成员函数和私有成员函数没有访问权限
#include <iostream>
int f (void)
{
class A
{
public:
int sum()
{
cout << "i am public" << endl;
return 0;
}
protected:
int time()
{
cout << "i am protected" << endl;
return 0;
}
private:
int now()
{
cout << "i am private" << endl;
}
};
A a;
a.sum();
a.time(); // error
a.now(); // error
}
int main(void)
{
f();
return 0;
}
4.局部类的嵌套
#include <iostream> using namespace std; void func( void )
{ //函数func()内定义一个局部类MyClass class MyClass
{ public: void f1( void )
{
cout << "MyClass::f1( )" << endl;
} //----------------------------------类中有类:局部类的嵌套 class Cla
{ public: void f2( void )
{
cout << "MyClass::Cla::f2( )" << endl;
}
}; //------------------------------------------ }; //访问局部类中的成员函数 MyClass rs;
rs.f1( ); //访问局部类中的嵌套类的成员函数 MyClass::Cla cl;
cl.f2( );
} int main( void )
{
func( );
system( "PAUSE" ); return EXIT_SUCCESS;
}
5.类中有类
#include <iostream> using namespace std; class A
{ public: void funA( void )
{
cout << "A::fA( )" << endl;
} //---------------------类中有类:在另一个类内部定义一个类 class B
{ public: void funB( void )
{
cout << "B::fB( )" << endl;
}
}; //------------------------------- }; int main( void )
{
A objA; //外部类实例化一个对象objA objA.funA( ); //objA对象调用类A成员函数 A::B objB; //内部类实例化一个对象objB objB.funB( );//objB对象调用类B成员函数 system( "PAUSE" ); return EXIT_SUCCESS;
}
6.
阅读(707) | 评论(0) | 转发(0) |