Why aren't we allowed to access a private static member outside class without public member function?

Rahul Jain

This may be a silly doubt but I am not able to understand that why I am not able to access a private static data member outside the class when I am allowed to define it.

For ex: in the following code:

class foo
{
    static int A;
    public:
       int getA{return A;}
};
//This is allowed
int foo:A=0;
int main()
{
   //This gives error
   cout<<foo:A;
}
vsoftco
int foo::A = 0;

allocates storage for the member variable A, and initializes it with 0 (actually a static is by default initialized with 0, so the assignment is superfluous). You do this only once in the implementation .cpp file. Then everyone will be able to instantiate your class without any linker issues. Note that you cannot do this again, i.e. assigning later foo::A = 42; will not compile, so you don't break any access rules. The fact that you must explicitly allocate storage is a language rule, which in my opinion creates more confusion (I'd make the compiler automatically allocate the storage when you declare the static).

So, to conclude, being allowed to define a private member is by far not as dangerous as being able to later access it/modify it, and is very different from the latter. The object is already sealed for the outside world once the member has its storage allocated.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Why can't this public member function call decltype on a private struct member declared inside the class?

From Dev

How to access private static variable in static member function of another class?

From Dev

Why is it not possible to define a static (public) member of a class in the main() function?

From Dev

Private function member called outside of class

From Dev

Why can't a PRIVATE member function be a friend function of another class?

From Dev

Static Template function access static class member

From Dev

Public class, but private member variables?

From Dev

Public class, but private member variables?

From Dev

Access to private static function during static member initialization

From Dev

How to access private static class member inherited from a template class?

From Dev

How do i get a class member function to have access to another class member function's private member?

From Dev

How can I access public member of private package class?

From Dev

How can I access public member of private package class?

From Dev

Private static member in base class

From Dev

Return private static member of a class

From Dev

Using friend function, can we overwrite the private member of the class?

From Java

How to access a public static final member in a Java class from Scala?

From Dev

How to access a public static final member in a Java class from Scala?

From Dev

can we access static member variable of pure virtual class?

From Dev

Why does a static data member need to be defined outside of the class?

From Dev

Friend member function cannot access private member

From Dev

Why can't I access a member of this class?

From Dev

Error when using public member function to access private member variable: Variable "was not declared in this scope"

From Dev

Why can't we use auto in template static member initialization?

From Dev

Accessing C++ class public member function from private struct data member

From Dev

Why can't we use nested type through class-member-access expression?

From Dev

Access private member variables through public methods

From Dev

Accessing Private Member Without Public Accessor in Java

From Dev

Why can't I overload C++ conversion operators outside a class, as a non-member function?

Related Related

  1. 1

    Why can't this public member function call decltype on a private struct member declared inside the class?

  2. 2

    How to access private static variable in static member function of another class?

  3. 3

    Why is it not possible to define a static (public) member of a class in the main() function?

  4. 4

    Private function member called outside of class

  5. 5

    Why can't a PRIVATE member function be a friend function of another class?

  6. 6

    Static Template function access static class member

  7. 7

    Public class, but private member variables?

  8. 8

    Public class, but private member variables?

  9. 9

    Access to private static function during static member initialization

  10. 10

    How to access private static class member inherited from a template class?

  11. 11

    How do i get a class member function to have access to another class member function's private member?

  12. 12

    How can I access public member of private package class?

  13. 13

    How can I access public member of private package class?

  14. 14

    Private static member in base class

  15. 15

    Return private static member of a class

  16. 16

    Using friend function, can we overwrite the private member of the class?

  17. 17

    How to access a public static final member in a Java class from Scala?

  18. 18

    How to access a public static final member in a Java class from Scala?

  19. 19

    can we access static member variable of pure virtual class?

  20. 20

    Why does a static data member need to be defined outside of the class?

  21. 21

    Friend member function cannot access private member

  22. 22

    Why can't I access a member of this class?

  23. 23

    Error when using public member function to access private member variable: Variable "was not declared in this scope"

  24. 24

    Why can't we use auto in template static member initialization?

  25. 25

    Accessing C++ class public member function from private struct data member

  26. 26

    Why can't we use nested type through class-member-access expression?

  27. 27

    Access private member variables through public methods

  28. 28

    Accessing Private Member Without Public Accessor in Java

  29. 29

    Why can't I overload C++ conversion operators outside a class, as a non-member function?

HotTag

Archive