views:

184

answers:

3

In C++, is it possible to have a child class "hide" a base class' static fields and methods? (i.e. A has a field named ABC of type int, B:A and B has a field named ABC of type int)

+2  A: 

Perhaps:

class B : private A
{
...
};

This will hide everything though, not just statics.

Andrew
Missing ending semicolon. ;D
strager
Fixed. Thanks, @strager.
Andrew
+1  A: 

Do you want to privately inherit?

class B : private A {
    // ...
};
strager
+6  A: 
#include <iostream>

using namespace std;

class A{
     public:
    static int a;
};

class B: public A{
     public:
      static  int a; // hide base member

 };

int A::a;
int B::a;
int main(){
    A::a=10;
    B::a=20;

    B k;
    cout << "\n" << B::a << k.a;
 return 0;
}
adatapost
Oh I don't even have to do anything? It just works?
jameszhao00
Yes it will be hidden (but clients can still access the member of base class if they specify the base class name explicitly, for example `A::a`).
ChrisW
Or `B::A::a` (which is the same thing)
Pavel Minaev