我有一堂课:
class Nothing
{
/// Constructor, Destructor, Copy and Assignment
public:
Nothing();
~Nothing();
Nothing(const Nothing& clone);
/// Operators
const Nothing& operator=(const Nothing& other);
/// Static Members
private:
static unsigned long long id_counter;
static unsigned long long instance_counter;
};
Nothing::Nothing()
{
m_name.clear();
id_counter ++;
m_id = id_counter;
instance_counter ++;
}
Nothing::~Nothing()
{
m_name.clear();
instance_counter --;
}
Nothing::Nothing(const Nothing& other)
{
}
unsigned long long Nothing::id_counter = 0;
unsigned long long Nothing::instance_counter = 0;
请注意,我使用 unsigned long long 来计算类的实例。我应该改用 std::size_t 吗?
顺便说一句:如果我有一个类的实例,我会做这样的事情:
Nothing instance;
instance(Nothing()); // Calling copy constructor
会在调用复制构造函数之前调用析构函数吗?问的原因是我需要在我id_counter ++;
的instance_counter ++;
复制构造函数内部吗?