static in classes

This is a discussion on static in classes within the C++ Programming forums, part of the General Programming Boards category; hi all, i want to make a class which can count how many objects it has. i thougt to use ...

  1. #1
    Registered User
    Join Date
    May 2003
    Posts
    17

    static in classes

    hi all,
    i want to make a class which can count how many objects it has.
    i thougt to use static and one initilaize aftert that incrementing it.

    Code:
    class x
    {
    public:
    	int no;
    	int give_no;
    	x();
    };
    
    x::x()
    {
    	no=0;
    	give_no++;
    	no= give_no;
    }
    but it gives me error. where am i wrong?

  2. #2
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,672
    Code:
    class x
    {
    public:
        int no;
        static int give_no;
        x();
    };
    
    // You need to declare an instance of each static variable used in a particular class
    int x::give_no = 0;
    
    x::x()
    {
        no = ++give_no;
    }
    I used to be an adventurer like you... then I took an arrow to the knee.

  3. #3
    Registered User
    Join Date
    May 2003
    Posts
    17
    aff scope operator, thanks. i have a terrible headache.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. static keyword and headers. help me understand.
    By cmay in forum C Programming
    Replies: 4
    Last Post: 04-17-2009, 03:19 PM
  2. sharing a static varable across classes
    By steve1_rm in forum C++ Programming
    Replies: 2
    Last Post: 01-31-2009, 12:17 AM
  3. Problem with Free.
    By chakra in forum C Programming
    Replies: 9
    Last Post: 12-15-2008, 10:20 AM
  4. static object never inits
    By krappa in forum C++ Programming
    Replies: 10
    Last Post: 10-30-2008, 12:04 PM
  5. Static functions.... why?
    By patricio2626 in forum C++ Programming
    Replies: 4
    Last Post: 04-02-2007, 08:06 PM

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