1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43
|
class C1
{
public:
C1(void) : _objectNumber(_objectCount++) {}
~C1(void) { --_objectCount; }
public:
std::size_t objectNumber(void) { return _objectNumber; }
private:
static std::size_t _objectCount;
std::size_t _objectNumber;
};
std::size_t C1::_objectCount = 0;
class C2
{
public:
C2(C1* c1) : _objectNumber(_objectCount++) { _c1 = c1; }
~C2(void) { --_objectCount; }
public:
std::size_t test(void) { return _c1->objectNumber(); }
bool isValid(void) { return _c1!=NULL; }
private:
static std::size_t _objectCount;
std::size_t _objectNumber;
C1* _c1;
};
std::size_t C2::_objectCount = 0;
int main(int argc, char* argv[], char* argenv[])
{
C1* c1 = new C1();
C2 c2(c1);
std::cout<<c2.isValid()<<std::endl;
std::cout<<c2.test()<<std::endl;
delete c1;
c1 = NULL;
std::cout<<c2.isValid()<<std::endl;
std::cout<<c2.test()<<std::endl;
return EXIT_SUCCESS;
} |
Partager