C ++ variable with the same name, context: global and private,

In the following code, g ++ gives this error: 1.cpp: In a member function, void W::test()': 1.cpp:6: error:int F :: glob 'is private 1.cpp: 19: error: in this context

But one should not declare globally here the variable 'glob' "private" "glob" is used?

   #include <iostream.h>

    int glob;
    class F
    {
        int glob;
        public:
        void readIt()
        {
            cin >> glob;
        }
    };

    class W : public F
    {
        public:
            void test()
            {
                glob--;
            }
    };

    int main()
    {
    }
+3
source share
4 answers

, . F::glob glob W::test(), . W::test() F::glob, . ::glob, - "" ( ).

+10

private glob , use:: glob ,

+5

::glob--;. .

+4

A class member will be used here to access the global use of the variable: operator.

+2
source

All Articles