Talk About Network

Google


Register and Login
Nick
Password
Register create new account Sign up is FREE and you can post replies, new topics, bookmark posts and more!
Recover lost password


Programming > C - C++ Learning > very newbie que...
Latest [ Topics | Posts ] Archive Post A New Topic Post a Reply
<< Topic < Post Post 1 of 4 Topic 4085 of 4218
Post > Topic >>

very newbie question about object creation count

by john <johnmortal.forums@[EMAIL PROTECTED] > Mar 26, 2008 at 12:46 PM

I have not been able to figure out why this ends up with a negative
object count (the class
counts how many objects there are). I am sure this is obvious to
everyone else. Could someone please
point out why the count is not correct. Sorry to ask such a dumb
question.

It gives the output:

Constructor called, so we have 1 TestClass objects.
Destructor called so we will have 0 TestClass objects.
Destructor called so we will have -1 TestClass objects.

Here is the code:

#include<iostream>


class TestClass {
       public:
               static int activeObjects; // How many of this type of
object were constructed

               TestClass() {      // default constructor
                       activeObjects ++;
                       std::cout << "Constructor called, so we have "
<< activeObjects << " TestClass objects.\n";
               }
               TestClass( const TestClass & rhs ) {      // copy
constructor
                       activeObjects ++;
                       std::cout << "Copy constructor called, so we
have " << activeObjects << " TestClass objects.\n";
               }
               ~TestClass() {      // destructor
                       activeObjects--;
                       std::cout << "Destructor called so we will have
" << activeObjects << " TestClass objects.\n";
               }
};

int TestClass::activeObjects = 0;

int main() {
       TestClass object = TestClass();
       object.~TestClass();

}
 




 4 Posts in Topic:
very newbie question about object creation count
john <johnmortal.forum  2008-03-26 12:46:15 
Re: very newbie question about object creation count
"Jim Langston"   2008-03-26 13:13:00 
Re: very newbie question about object creation count
john <johnmortal.forum  2008-03-26 13:17:57 
Re: very newbie question about object creation count
"Vijaya Kumar B.H.&q  2008-03-31 13:35:50 

Post A Reply:
  Go here to Signup

AddThis Feed Button


About - Advertising - Contact - Frequently Asked Questions - Privacy Policy - Terms of Use - Signup

Contact
tan12V112 Fri Jul 25 12:51:24 CDT 2008.