0

lets say I have a base class B and 2 classes that derived from it:

class B
{
public:
     A_type* Check_Memory(const LeafInterface* leaf = 0);
}

class D1 : public B {}

class D2 : public B
{
    A_type* Check_Memory(const LeafInterface* leaf, U32* faults);
}

now, when I call:

D1 d1;
d1.Check_Memory(leaf);

it call the function from the base class as expected,

but when I do the same with:

D2 d2;
d2.Check_Memory(leaf);

instead of calling the function from the base class, I get the following error: function does not take 1 arguments.

does my only solution is declaring another function in D2 that call the base function? is there more elegant way in C++ for doing it?

Aviv A.
  • 697
  • 5
  • 11

1 Answers1

0

It's because D2::Check_Memory hides B::Check_Memory.

When you call the function, compiler find where the function is at first, after that, it tries to call the function, and if the number of proper function is not 1, it starts to resolve function overloading.

When compiler compiles d2.Check_Memory(leaf);, it tries to find what Check_Memory is at first. It's easy - d2 is class D2, and it has A_type* Check_Memory(const LeafInterface* leaf, U32* faults);. After that, compiler tries to call this, but as you know, it's impossible. More unfortunately, there's no function overloading (since A_type* Check_Memory(const LeafInterface* leaf = 0); is in class B.). So the compile error occurs.

To avoid that, renaming your function's name is the best (the easier code is, the better code is). However, if you can't do, use using.

class D2 : public B
{
    using B::Check_Memory;
    A_type* Check_Memory(const LeafInterface* leaf, U32* faults);
}

In that case, A_type* Check_Memory(const LeafInterface* leaf, U32* faults); doesn't hide B::Check_Memory, so the resolving function overloading will be started.

ikh
  • 10,119
  • 1
  • 31
  • 70