Base class function name hiding, thoughts?

Hey,

I'm currently unsure about using base class function name hiding, do you guys consider this as bad somehow? I'm currently undecided.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
class Base
{
public:
    void Activate() { //activates somth... }
}

class Derived : public Base
{
  public:
     void Activate()
     {
       Base::Activate(); //activate someth
       enable = true; // and more
     }

  private:
  bool enable;
}


Having the same signature, but not using virtual functions.
For me it seems to be fine calling Derived::Activate() when directly using the Dervied object and not getting the benefits of polymorphism for base class ptrs.

Last edited on
What worries me is if someone calls Activate() on a Derived object through a Base pointer or reference.
@Peter87 yeah it wouldn't do the additional stuff which I think would be still ok? or could be confusing to others ...

I could imagine that someone could think the function actually should get called since its not virtual but at the end it won't. (at least not directly)
Last edited on
good, bad, … depends on what you are trying to do. Who is going to be using it? The trap in code like this seems to be the designer's intended use of something vs the user's creativity. You write it, test it, it does what you want and so on perfectly. Then your buddy gets it and it does not work in his code because he used it differently. That may or may not be a deal breaker -- maybe he didn't read the documentation or something. But it certainly limits the ways the code can be reused. Sometimes, such limits are intended, for whatever reasons. Sometimes they are just an annoying limitation.

your design seems like a nice simple one for basic code where the base is used in a bunch of deriveds and the only purpose of the base is to factor common items out of similar but different classes. If the base is truly unusable or truly pointless to use by itself (lacking key functionality that the derived classes have to supply), its fine. If the base could be useful, its limiting.

I don't want to read too much into your contrived example, but since it is what you gave us...

The fact that you call Base::Activate() from within Derived::Activate() looks like how you would write a virtual function. Again, I realize this is a contrived example, but it has the feel of what should be a virtual function.

If you were iterating through a std::vector<Base*>, would you only want to call the Base::Activate on each of the objects? You are unable to get to the derived class functionality without introspection and casting.

Again, it's a contrived example, but when would you want to call Activate() on an object of type Derived and not set the enable flag to true? If never, then Activate should be a virtual function.

As others have said, you can do what you are showing, and there may be good reasons to do so, but you better have a good reasons to do so and document the behavior really well.
Topic archived. No new replies allowed.