Thread: static variables inside of a class.. possible?

  1. #1
    Registered User
    Join Date
    Dec 2002
    Posts
    221

    static variables inside of a class.. possible?

    Code:
    #include <iostream.h>
    #include <iomanip.h>
    #include <stdlib.h>
    
    using namespace std;
    
    class Counter
        {
            private:
               static int serial;
               public:
                    Counter():serial()
                        {inc_counter();}
                    void show_serial()
                    {
                        cout << "I am object number " << serial << endl;
                    }
                    void inc_counter()
                    {
                        serial++;
                    }
        };
    
    int main()
    {
        Counter s1, s2, s3;
    
        s1.show_serial();
        s2.show_serial();
        s3.show_serial();
        system("PAUSE");
        return 0;
    }
    basically, i want to be able to keep a "serial" number going inside of each object that will display a message saying "i am object number x" (x for whatever number)
    the only way i know how to do this is by static, but its not working
    any help with the code?

    i wanna try to do everything within the class itself. i know i'd be easier if i passed it a number once its constructed, but eh
    i think the quesiton is saying for everything to be inside the class

  2. #2
    Registered User
    Join Date
    Dec 2002
    Posts
    221
    i also know somewhere in there i need to initilize it to 0
    but static int serial =0 doesnt work

    this is my first work with classes ever

  3. #3
    Programming Sex-God Polymorphic OOP's Avatar
    Join Date
    Nov 2002
    Posts
    1,078
    Code:
    #include <iostream>
    
    class Counter
    {
    public:
      Counter();
      void show_serial();
    protected:
      int inc_counter();
    private:
      static int NumObjects;
      int serial;
    };
    
    Counter::Counter()
      : serial( inc_counter() )
    {
    }
    
    void Counter::show_serial()
    {
      std::cout << "I am object number " << serial << std::endl;
    }
    
    int Counter::inc_counter()
    {
      return NumObjects++;
    }
    
    int Counter::NumObjects = 0;
    
    int main()
    {
      Counter s1, s2, s3;
    
      s1.show_serial();
      s2.show_serial();
      s3.show_serial();
      return 0;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Pointer to a class inside the WindowsProcedure function
    By like_no_other in forum Windows Programming
    Replies: 3
    Last Post: 06-01-2009, 12:52 PM
  2. Replies: 10
    Last Post: 07-26-2008, 08:44 AM
  3. deriving classes
    By l2u in forum C++ Programming
    Replies: 12
    Last Post: 01-15-2007, 05:01 PM
  4. Question about Static variables and functions.
    By RealityFusion in forum C++ Programming
    Replies: 2
    Last Post: 10-14-2005, 02:31 PM
  5. Using bitset class inside another
    By Dhekke in forum C++ Programming
    Replies: 3
    Last Post: 07-26-2005, 06:50 AM