Jump to content

how the stat variable of a class is defined/initialized outside the class in C++?

- - - - -

  • Please log in to reply
3 replies to this topic

#1
jackson6612

jackson6612

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 304 posts
Hi

I know the code below is faulty (actually I have made it so!). I just wanted to know how the stat variable of a class is to be defined/initialized outside the class. I believe I can get away with the keyword unsigned const static because they doesn't define the type of the variable rather they are simply modifiers (just like adjectives).

Should it be written as :
int foo::count = 0;
or,
[COLOR="#40E0D0"]unsigned const static[/COLOR] int count = 0;
?


// statdata.cpp

// static class data


#include <iostream>


using namespace std;


////////////////////////////////////////////////////////////////

class foo

   {

   private:

      unsigned const static int count;   //only one data item for all objects

                          //note: *declaration* only!

   public:

      foo()               //increments count when object created

         { count++; }

      int getcount()      //returns count

         { return count; }

   };

//--------------------------------------------------------------


[B][COLOR="#FF8C00"]int foo::count = 0;[/COLOR][/B]       //*definition* of count

////////////////////////////////////////////////////////////////


int main()

   {

   foo f1, f2, f3;        //create three objects


   cout << "count is " << f1.getcount() << endl;  //each object

   cout << "count is " << f2.getcount() << endl;  //sees the

   cout << "count is " << f3.getcount() << endl;  //same value

   return 0;

   }


I'm an outright beginner, learning C++. Using Win XP Pro and Code::Blocks. Be nice to me, please.:)

#2
gregwarner

gregwarner

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 853 posts
  • Location:Arkansas
Declaring variables as "const" prohibits their being changed, so
count++;
will not compile.

The best way I can think of to accomplish what you're wanting to do is use a global variable to store the count.
Hofstadter's Law: It always takes longer than you expect, even when you take into account Hofstadter's Law.

– Douglas Hofstadter, Gödel, Escher, Bach: An Eternal Golden Braid


#3
jackson6612

jackson6612

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 304 posts

gregwarner said:

Declaring variables as "const" prohibits their being changed, so
count++;
will not compile.

The best way I can think of to accomplish what you're wanting to do is use a global variable to store the count.

Hi

I think you have missed the following quotes statement. :)

Quote

I know the code below is faulty (actually I have made it so!). I just wanted to know how the stat variable of a class is to be defined/initialized outside the class.

I'm an outright beginner, learning C++. Using Win XP Pro and Code::Blocks. Be nice to me, please.:)

#4
Flying Dutchman

Flying Dutchman

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 889 posts
  • Location:::1
No you can't get away with this, because compiler can't know you were thinking of that member variable, even if it has the same name. You need to use scope operator. The words itself mean this:
  • unsigned: you can't store negative numbers
  • const: you can't change it's value once it's been initialized
  • static: has more meanings, but in context of classes, it means all classes share this variable
If you want a counter to keep track of foo instances, then you have to remove const keyword, and add a destructor that will decrement that variable.
A conclusion is where you got tired of thinking.
#define class struct    // All is public.




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users