总结一下类型名定义变量

 

先看两个类的定义

class A{
static int struct_cnt;
public:
A( )
{
struct_cnt++;
}
public:
void Print()
{
printf("A is construced:%d\n",struct_cnt);
}
};
int A::struct_cnt = 0;


class B{
static int struct_cnt;
public:
B(int inpara)
{
struct_cnt++;
}
public:
void Print()
{
printf("B is construced:%d\n",struct_cnt);
}
};
int B::struct_cnt = 0;




再看两个类作为类型的操作:

    A* a = new A() ;
a->Print();
delete a;
a = new A;
a->Print();

static A b;
b.Print();

A c;
c.Print();

//B ba;//编译错误:错误 9 error C2512: “B”: 没有合适的默认构造函数可用
//但是如果B的构造函数有缺省参数则可以

//B ba();//错误,这里ba成为了一个函数了
//static B ba();错误,连函数定义都编译不过过去,因为不能有局部静态函数
B ba = B(0);
ba.Print();
B*pb;
pb = new B(0);
pb->Print();

 


输出结果:

posted on 2012-03-14 13:08  菜丝inside  阅读(234)  评论(0编辑  收藏  举报