池中物
全部博文(43)
分类: C/C++
2013-10-17 19:35:25
int calcSquare(int n)
{
printf("\n from outer!");
return n*n ;
}
class test
{
public:
int m_date;
float m_fDate;
int &m_outer;
const int m_const_value;
static int static_createTimes;
int calcSquare(int n);
static int getCreateTimes();
test(int outer=0):m_const_value(0),m_outer(outer)//常成员和应用成员必须列表初始化,且引用成员变量不能接受常量(m_outer(0)是错误的)
{
test::static_createTimes++;
}
test(int iDate,float fDate,int const_value ,int outer = 6);
void Print();
int getSquare(int n);
~test();
};
test::test(int iDate, float fDate, int const_value,int outer):m_date(iDate),m_fDate(fDate),m_const_value(const_value),m_outer(outer)
{
test::static_createTimes++;//static_createTimes++ 和 this->static_createTimes++;都可以,非静态成员函数可以调用静态成员变量
}
test::~test()
{
test::static_createTimes-- ;//可以域说明
}
int test::static_createTimes = 0;
int test::getCreateTimes()
{
return static_createTimes ;//静态成员函数不能使用类的非静态成员变量,
}
void test::Print()
{
printf("\n before :%d ->",static_createTimes);
this->static_createTimes++;//可以不作域说明
printf("\n after :%d\n",static_createTimes);
//printf("\nm_date = %d\n m_fDate = %0.2f\n m_const_value = %d\n static_creatTiems = %d",m_date,m_fDate,m_const_value,static_createTimes);
}
int test::calcSquare(int n)
{
return n*n;
}
int test::getSquare(int n)
{
printf("\n inner :calcSquare(%d)-> %d",n,calcSquare(n));
printf("\n outer :calcSquare(%d)-> %d",n,::calcSquare(n));//使用外部全局同名同参数函数时要加域说明,变量也是一样
return calcSquare(n);
}
void main()
{
test testA;
testA.getSquare(5);
testA.getCreateTimes() ;
testA.Print();
test testB(12,12.34f,12);
testB.getSquare(5);
testB.Print();
}