keyword 'static'
-
Is there a difference naming a static member variable of a class private?? how is a static public member variable different from a static public member variable??
I could guess your question. Static and private has totally different meanings. when you declare a variable private but not static, it means that it can be used within the same class. It cannot be accessed by other members(may be another class). and more importantly, every time an Object is instantiated, a new instance of the variable is created. But when you declare a varible private and also static. It's all the same but the last point, which when new objects are created, it's not instantiated anew, rather all objects share the variable. and when you want to access it, you cannot use the . or the -> operator, you should use the :: operator with the class name preceeding it. now you get it? note, you cannot put "static" in the private,protected,public list. it can coexist with all the three ;)
--[V]-- [My Current Status]
-
Is there a difference naming a static member variable of a class private?? how is a static public member variable different from a static public member variable??
don't confuse !!! static don't alter the visibility accessors (
public
,protected
,private
). it only tells the compiler that the member declared as static will exist once and will be shared between all the instances of the class... do you get the difference then ?
TOXCCT >>> GEII power
[VisualCalc 3.0 updated ][Flags Beginner's Guide new! ]
-
Is there a difference naming a static member variable of a class private?? how is a static public member variable different from a static public member variable??
The static-ness and access of a member variable aren't mutually exclusive. All
static
means is that there is only one copy of that member for all instances of the class. public/protected/private access all work the same as with non-static
members.--Mike-- Visual C++ MVP :cool: LINKS~! Ericahist | PimpFish | CP SearchBar v3.0 | C++ Forum FAQ
-
I could guess your question. Static and private has totally different meanings. when you declare a variable private but not static, it means that it can be used within the same class. It cannot be accessed by other members(may be another class). and more importantly, every time an Object is instantiated, a new instance of the variable is created. But when you declare a varible private and also static. It's all the same but the last point, which when new objects are created, it's not instantiated anew, rather all objects share the variable. and when you want to access it, you cannot use the . or the -> operator, you should use the :: operator with the class name preceeding it. now you get it? note, you cannot put "static" in the private,protected,public list. it can coexist with all the three ;)
--[V]-- [My Current Status]
I think i havent asked the question clearly what i actually ment to ask is : if a member is declared private then it implies, it cannot be accessed by any objects directly. But in the case of static member varaibles, they can be accessed by using 'class name' followed by '::' operator. So wouldnt a private menber varaible declared static loose its significance as private if it can be accessed by by the :: operator????
-
don't confuse !!! static don't alter the visibility accessors (
public
,protected
,private
). it only tells the compiler that the member declared as static will exist once and will be shared between all the instances of the class... do you get the difference then ?
TOXCCT >>> GEII power
[VisualCalc 3.0 updated ][Flags Beginner's Guide new! ]
I think i havent asked the question clearly what i actually ment to ask is : if a member is declared private then it implies, it cannot be accessed by any objects directly. But in the case of static member varaibles, they can be accessed by using 'class name' followed by '::' operator. So wouldnt a private menber varaible declared static loose its significance as private if it can be accessed by by the :: operator????
-
The static-ness and access of a member variable aren't mutually exclusive. All
static
means is that there is only one copy of that member for all instances of the class. public/protected/private access all work the same as with non-static
members.--Mike-- Visual C++ MVP :cool: LINKS~! Ericahist | PimpFish | CP SearchBar v3.0 | C++ Forum FAQ
I think i havent asked the question clearly what i actually ment to ask is : if a member is declared private then it implies, it cannot be accessed by any objects directly. But in the case of static member varaibles, they can be accessed by using 'class name' followed by '::' operator. So wouldnt a private menber varaible declared static loose its significance as private if it can be accessed by by the :: operator????
-
I think i havent asked the question clearly what i actually ment to ask is : if a member is declared private then it implies, it cannot be accessed by any objects directly. But in the case of static member varaibles, they can be accessed by using 'class name' followed by '::' operator. So wouldnt a private menber varaible declared static loose its significance as private if it can be accessed by by the :: operator????
If it is
private
, it means it. No matter it is static or not. You cannot access even with a :: operator.
--[V]-- [My Current Status]
-
If it is
private
, it means it. No matter it is static or not. You cannot access even with a :: operator.
--[V]-- [My Current Status]
thank u! :)
-
Is there a difference naming a static member variable of a class private?? how is a static public member variable different from a static public member variable??
namaskaaram wrote:
how is a static public member variable different from a static public member variable??
From my vantage point, they are identical.
"The largest fire starts but with the smallest spark." - David Crow
"Judge not by the eye but by the heart." - Native American Proverb
-
I think i havent asked the question clearly what i actually ment to ask is : if a member is declared private then it implies, it cannot be accessed by any objects directly. But in the case of static member varaibles, they can be accessed by using 'class name' followed by '::' operator. So wouldnt a private menber varaible declared static loose its significance as private if it can be accessed by by the :: operator????
namaskaaram wrote:
So wouldnt a private menber varaible declared static loose its significance as private if it can be accessed by by the :: operator????
Have you tried it to see?
"The largest fire starts but with the smallest spark." - David Crow
"Judge not by the eye but by the heart." - Native American Proverb
-
I think i havent asked the question clearly what i actually ment to ask is : if a member is declared private then it implies, it cannot be accessed by any objects directly. But in the case of static member varaibles, they can be accessed by using 'class name' followed by '::' operator. So wouldnt a private menber varaible declared static loose its significance as private if it can be accessed by by the :: operator????
class test { private: static int foo; };
int test::foo = 1;main()
{
int x = test::foo;
}error C2248: 'foo' : cannot access private member declared in class 'test'
--Mike-- Visual C++ MVP :cool: LINKS~! Ericahist | PimpFish | CP SearchBar v3.0 | C++ Forum FAQ