How do you hide some public members of base class?

Hi all,

Is there a way to hide only some of the public members of a base class? It so happens that my child class makes it dangerous to access some of the members of the base class, but not all. My code is:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
template <class Object>
class ListBasicObjects
{
public:
  Object* TheObjects;
  void AddObjectOnTop(Object& o);
};

//In the class below I want to hide the 
//AddObjectOnTop function but still have TheObjects
//remain as public
template <class Object>
class HashedListBasicObjects : public ListBasicObjects<Object>
{
public:
  void AddObjectOnTopHash(Object& o);
};


How am I supposed to do this?

Thanks!
Last edited on
Don't try to "hide" the parent class's functions. Make them virtual and give the child class its own definitions:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
template <class Object>
class ListBasicObjects
{
public:
  Object* TheObjects;
  virtual void AddObjectOnTop(Object& o);
};

// child which redefines AddObjectOnTop
//  presumably, this version would do a hashing process (no need to
//  rename the function to 'AddObjectOnTopHash')
template <class Object>
class HashedListBasicObjects : public ListBasicObjects<Object>
{
public:
  virtual void AddObjectOnTop(Object& o);
};
Thanks a lot! Will do it as you say!

I also need to call the base class function from the child ( but in a specific/tricky fashion, as the name of the child class suggests). What is the syntax for that?

[Edit:] just found the answer - point 20.5 in:

http://www.parashift.com/c++-faq-lite/virtual-functions.html#faq-20.5
Last edited on
Although it might not be a good idea, I think you can re-declare public base class members private in derived classes...
Topic archived. No new replies allowed.