A: 

If you don't want to make it pure virtual and thereby force the implementation, you might just make the body of foo() in the Base class just immediately throw an exception. This serves the purpose of forcing any implementors and users to implement an override, or have it blow up when they try to use it.

I've seen this done before, and while it's a bit ugly, it very certainly works.

McWafflestix
+1  A: 

"void foo() const" and "void foo()" are two completely different functions as far as C++ is concerned. That's why you don't see the Derived's "foo" hiding the "foo" from the Base.

florin
That's not true, if a base class defines a function then it hides all functions with the same name in the base class even if they have different parameters (don't believe me? try it), the way to make these functions visible again is to use "using base::foo;"
Motti
+3  A: 

Simply by defining a member function foo in the derived class, you have hidden all of the foo functions in the base class.

You'll need to refine the question a little - are you concerned that the foo in the derived class is not a proper replacement for the foo in the base class? I'm having a hard time trying to determine what you're really asking for.

Edit: Based on your edit and additional comments, I think you have a misunderstanding of how hiding works in C++. In this case, it doesn't matter that one function is const and the other one isn't - once C++ finds a function foo in the derived class, it stops looking anywhere else! This is usually a huge trap for people. Consider the following:

class Base
{
    void foo(double d)
    {
        cout << d;
    }
};
class Derived : public Base
{
    void foo(int i)
    {
        cout << i;
    }
};

Derived obj;
obj.foo(123.456);

What do you think the output is? It's 123! And you probably got a compiler warning telling you the double value was going to be truncated. Even though the function signature that takes a double is obviously a better match, it's never considered - it has been hidden.

Mark Ransom
I have edited the question let me know if it helps
kal
+1  A: 

Are you sure you want to HIDE foo() from Base? If you want to exploit polymorphism you need to make the base version of foo() virtual. It does not have to be pure, though. Otherwise you get static binding - do you want that?

TheFogger
A: 

If I have understood your question properly, you are asking how to make sure that Derived classes override the method compulsorily, without making the method pure in Base class.

By making sure all of your derived classes to provide the implementation of the method, IMHO you are hinting that the method in Base class is really not used for any purpose. The objects must invoke any of the Derived class version of the method foo(). Hence, the method should be made pure virtual in my opinion. I don't think any other way to achieve this, I believe.

Also, note that in your example you have changed the signature of foo() in Base and Derived. This makes the Base class method to be invisible in Derived class. Unless you use using Base::foo, the method foo() will not be visible to Derived class.

aJ
A: 

In Base::foo(), you could do the following:

assert (dynamic_cast<const Derived*> (this) == NULL);

But it has three problems:

  1. It requires a change in the Base class, which should be closed to modification.
  2. It may be stronger than you need, since you may want to permit Base::foo() to be called from a Base object or explicitly.
  3. It uses RTTI, which means a vtbl, which might be why you didn't want to user virtual functions to begin with.
JohnMcG