0

I am working on a c++ project, and facing a design issue currently. I would appreciate it if someone can give me some suggestions. Basically, I have a base class Base, and a subclass Derived as below. Class Derived can do something that class Base cannot do.

class Base
{
public:
   virtual bool IsCapableOfDoingA() {return false;}
}

class Derived: public Base
{
public:
   bool IsCapableOfDoingA() {return true;}
   void DoA();
}

In another place where I have a pointer of type class Base.

void functionA(Base *pBase)
{
   if (pBase && pBase->IsCapableOfDoingA())
   {
      Derived *pDerived = static_cast<Derived*>(pBase);
      pDerived->DoA();
   }

}
10
  • 1
    What are you doing with Base and Derived that needs dynamic polymorphism in the first place? Without some context, there's no way to answer this question.
    – ildjarn
    Feb 15, 2012 at 19:59
  • In your question you ask for an alternative to dynamic_cast, but what's wrong with it?
    – user743382
    Feb 15, 2012 at 19:59
  • This is impossible to answer without some more context. But as you've guessed, you should almost certainly reconsider your design if you're in a situation where you need to query what member functions exist. Feb 15, 2012 at 20:02
  • 2
    Is my impression or it should be static_cast<Derived*>(pBase) (with the pointer)?
    – dsign
    Feb 15, 2012 at 20:04
  • 1
    Can you just make a public virtual configure() function, and let each subclass decide how best to do that? If it involves doing A, then those that can, will. Feb 15, 2012 at 20:37

4 Answers 4

4

The usual way is to add the function to the base class.

virtual void DoA() { throw std::runtime_error("Not implemented"); }
1
  • Depends - if it's reasonable and common to do nothing, I'd just write virtual void DoA() {}. Note that the question doesn't contain a throw in an else branch.
    – MSalters
    Feb 16, 2012 at 9:58
2

In your design, is it ok for DoA to return a success code of some sort? You could replace IsCapableOfDoingA with DoA itself, and have the Base version simply return false (or other suitable error code). Child objects capable of doing A can override this function with a proper implementation.

You're right that dynamic_cast is often a design smell. With some more context we might be able to provide a better answer. My first thought is to really make sure a Derived IS-A Base if you're going to be polymorphically asking Bases for things they might not be able to do.

2
  • It's ok for Base to return a false since it's not capable of doing A. But since Base is not capable of doing A, I am figuring if it really makes sense to provide this interface to Base. IIRC, it's design problem discussed in book "Effect C++". The author gave an example that a Bird can fly or cannot. So it's better to not provide a fly() for class Bird, But derive a subclass FlyableBird that has a fly() member method.
    – Kevin W
    Feb 15, 2012 at 20:33
  • Yep, it's Item #32 from the 3rd edition of Effective C++. Scott gives suggestions (Mark's answer being one of them) but no absolute right answers. Feb 15, 2012 at 20:35
1

dynamic_cast will return NULL if the object doesn't have the appropriate type (assuming that the base class defines at least one virtual method -- usually you should at least make the base class's destructor virtual). So you can use the following idiom:

void functionA(Base *pBase)
{
   if (Derived *pDerived = dynamic_cast<Derived*>(pBase)) {
      pDerived->DoA();
   }    
}

If pBase doesn't have type Derived, then pDerived will be 0 (false), so the body of the if statement will be skipped.

0

Here's another idea. You can split the enhanced functions into an interface and have a method to return the interface pointer.

class InterfaceA
{
public:
    virtual ~InterfaceA() {}
    virtual void DoA() = 0;
};

class Base
{
public:
   virtual InterfaceA* GetAInterface() {return NULL;}
};

class Derived: public Base, InterfaceA
{
public:
   InterfaceA* GetAInterface() {return this;}
   void DoA();
};

void functionA(Base *pBase)
{
   InterfaceA* pA = pBase ? pBase->GetAInterface() : NULL;
   if (pA)
      pA->DoA();
}
1
  • Thanks Mark for your suggestions. static_cast is not needed in your solution.
    – Kevin W
    Feb 15, 2012 at 20:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.