// 用静态数据成员实现数据共享
#include "stdafx.h"
#include <iostream>
class CountedClass
{
static int count;
public:
CountedClass() { ++count; }
CountedClass(const CountedClass&) { ++count; }
~CountedClass() { --count; }
static int getCount()
{
return count;
}
};
int CountedClass::count = 0;
int main()
{
CountedClass a;
std::cout<<CountedClass::getCount()<<std::endl;
CountedClass b;
std::cout<<CountedClass::getCount()<<std::endl;
{
CountedClass c(b);
std::cout<<CountedClass::getCount()<<std::endl;
a = c;
std::cout<<CountedClass::getCount()<<std::endl;
}
std::cout<<CountedClass::getCount()<<std::endl;
system("pause");
return 0;
}
// 当其计数类为父类时 如此共享数据失败
#include "stdafx.h"
#include <iostream>
class CountedClass
{
static int count;
public:
CountedClass() { ++count; }
CountedClass(const CountedClass&) { ++count; }
~CountedClass() { --count; }
static int getCount()
{
return count;
}
};
int CountedClass::count = 0;
class CountedClass1:public CountedClass {};
class CountedClass2:public CountedClass {};
int main()
{
CountedClass1 a;
std::cout<<CountedClass::getCount()<<std::endl; // 1
CountedClass1 b;
std::cout<<CountedClass::getCount()<<std::endl; // 2
CountedClass2 c;
std::cout<<CountedClass::getCount()<<std::endl; // 3 Error
system("pause");
return 0;
}
// 使用模板时,可以实现子类每一个类都有一个static int 静态变量
#include "stdafx.h"
#include <iostream>
using namespace std;
template<class T>
class CountedClass
{
static int count;
public:
CountedClass() { ++count; }
CountedClass(const CountedClass&) { ++count; }
~CountedClass() { --count; }
static int getCount()
{
return count;
}
};
template<class T>
int CountedClass<T>::count = 0;
class CountedClass1:public CountedClass<CountedClass1> {};
class CountedClass2:public CountedClass<CountedClass2> {};
int main()
{
CountedClass1 a;
std::cout<<CountedClass<CountedClass1>::getCount()<<std::endl; // 1
CountedClass2 b;
std::cout<<CountedClass<CountedClass2>::getCount()<<std::endl; // 1
system("pause");
return 0;
}
#include "stdafx.h"
#include <iostream>
class CountedClass
{
static int count;
public:
CountedClass() { ++count; }
CountedClass(const CountedClass&) { ++count; }
~CountedClass() { --count; }
static int getCount()
{
return count;
}
};
int CountedClass::count = 0;
int main()
{
CountedClass a;
std::cout<<CountedClass::getCount()<<std::endl;
CountedClass b;
std::cout<<CountedClass::getCount()<<std::endl;
{
CountedClass c(b);
std::cout<<CountedClass::getCount()<<std::endl;
a = c;
std::cout<<CountedClass::getCount()<<std::endl;
}
std::cout<<CountedClass::getCount()<<std::endl;
system("pause");
return 0;
}
// 当其计数类为父类时 如此共享数据失败
#include "stdafx.h"
#include <iostream>
class CountedClass
{
static int count;
public:
CountedClass() { ++count; }
CountedClass(const CountedClass&) { ++count; }
~CountedClass() { --count; }
static int getCount()
{
return count;
}
};
int CountedClass::count = 0;
class CountedClass1:public CountedClass {};
class CountedClass2:public CountedClass {};
int main()
{
CountedClass1 a;
std::cout<<CountedClass::getCount()<<std::endl; // 1
CountedClass1 b;
std::cout<<CountedClass::getCount()<<std::endl; // 2
CountedClass2 c;
std::cout<<CountedClass::getCount()<<std::endl; // 3 Error
system("pause");
return 0;
}
// 使用模板时,可以实现子类每一个类都有一个static int 静态变量
#include "stdafx.h"
#include <iostream>
using namespace std;
template<class T>
class CountedClass
{
static int count;
public:
CountedClass() { ++count; }
CountedClass(const CountedClass&) { ++count; }
~CountedClass() { --count; }
static int getCount()
{
return count;
}
};
template<class T>
int CountedClass<T>::count = 0;
class CountedClass1:public CountedClass<CountedClass1> {};
class CountedClass2:public CountedClass<CountedClass2> {};
int main()
{
CountedClass1 a;
std::cout<<CountedClass<CountedClass1>::getCount()<<std::endl; // 1
CountedClass2 b;
std::cout<<CountedClass<CountedClass2>::getCount()<<std::endl; // 1
system("pause");
return 0;
}