Wednesday, February 18, 2009

[Cpp-Programming] Re: Is there such thing called private static member data in C++?

On Wed, Feb 18, 2009 at 10:57 PM, Ramaraju naga <v.nagaramaraju@gmail.com> wrote:
there is no scope for static type varibale..it's always public only....

Thanks & Regards
Ramaraju
 

That's not true - static variables can certainly be private.  Try this simple example:

#include <iostream>
using namespace std;

class A
{
private:
   static int staticVar;
public:
   A() { staticVar++; }
   void printStaticVar() { cout << staticVar << endl; }
};

int A::staticVar = 0;

int main(int argc, char** argv)
{
   A a1;
   a1.printStaticVar();
   A a2, a3;
   a2.printStaticVar();
   //A::staticVar = 10;  //Error, A::staticVar is private
   a3.printStaticVar();
   return 0;
}

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "C++ Programming" group.
To post to this group, send email to Cpp-Programming@googlegroups.com
To unsubscribe from this group, send email to Cpp-Programming+unsubscribe@googlegroups.com
For more options, visit this group at http://groups.google.com/group/Cpp-Programming?hl=en
-~----------~----~----~----~------~----~------~--~---

0 Comments:

Post a Comment

<< Home