static variables inside of a class.. possible?

This is a discussion on static variables inside of a class.. possible? within the C++ Programming forums, part of the General Programming Boards category; Code: #include <iostream.h> #include <iomanip.h> #include <stdlib.h> using namespace std; class Counter { private: static int serial; public: Counter():serial() {inc_counter();} ...

  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, 01:52 PM
  2. Replies: 10
    Last Post: 07-26-2008, 09: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, 03:31 PM
  5. Using bitset class inside another
    By Dhekke in forum C++ Programming
    Replies: 3
    Last Post: 07-26-2005, 07:50 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21