4

According to this answer, dynamic_cast'ing a base class to derived class is fine, but he says this shows that there is a fundamental problem with the code logic.

I've looked at other answers and using dynamic_cast is fine since you can check the pointer validity later.

Now in my real problem the derived class has a GetStrBasedOnCP function which is not virtual (only the derived class has it) and I have to access it.

What is better, to create a virtual void GetStrBasedOnCP on the base class and make it virtual on the derived OR, to just cast the base class pointer to derived class?

Oh also notice that this is a unsigned int GetStrBasedOnCP so the base class must also return a value...

Community
  • 1
  • 1
Vinícius
  • 15,498
  • 3
  • 29
  • 53
  • 4
    Does it make sense for the base class you have the virtual function in it? – NathanOliver Dec 15 '15 at 15:35
  • No, this is a function that would only be necessary in the derived class, but in that answer he said there would be a `fundamental problem` and that got me worried. Note: damn I really enjoy how there are such experienced programmers here on S.O, NathanOliver's question shows he knew exactly what I was needing – Vinícius Dec 15 '15 at 15:38
  • 1
    Well remember that best practices cover the general case. There are times you need to do things you wouldn't normally do to get the code working. The key thing you need is clear, concise, understandable code. – NathanOliver Dec 15 '15 at 15:41
  • The idea is that the set of virtual functions of a base class describe the common interface of a group of classes deriving from it. The goal is to be able to use all special implementations uniformly. Part of the charm is that more specializations can be added long after the using part was implemented and compiled. (`shape->rotate(90)`, for whatever shape). If you start and implement special cases *in the using code* you violate that paradigm. But the post you cite also mentioned a general exception, namely weakly or "untyped" containers. Think Java collections before templates. – Peter - Reinstate Monica Dec 15 '15 at 15:46

3 Answers3

3

There are more than two answers to the "what is better" question, and it all depends on what you are modeling:

  • If the GetStrBasedOnCP function is logically applicable to the base class, using virtual dispatch is the best approach.
  • If having the GetStrBasedOnCP function in the base class does not make logical sense, you need to use an approach based on the actual type; you could use dynamic_cast, or
  • You could implement multiple dispatch, e.g. through a visitor or through a map of dynamic types.

The test for logical applicability is the most important one. If GetStrBasedOnCP function is specific to your subclass, adding it to the base class will create maintenance headaches for developers using and maintaining your code.

Multiple dispatch, on the other hand, gives you a flexible approach that lets you access statically typed objects. For example, implementing visitor pattern in your base class lets you make visitors that process the subclass with GetStrBasedOnCP function differently from other subclasses.

Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
2

Does it make sense for the base class you have to have the virtual function in it?

If it does not then you should not include the function in the base class. Remember that best practices cover the general case. There are times you need to do things you wouldn't normally do to get the code working. The key thing is you need is clear, concise, understandable code

NathanOliver
  • 171,901
  • 28
  • 288
  • 402
1

There's a lot of "it depends".

If you can guarantee that the base pointer is the correct child pointer, then you can use dynamic_cast.

If you can't guarantee which child type the base pointer is pointing to, you may want to place the function in the base class.

However, be aware that all children of the base class will get the functionality of whatever you place into the base class. Does it make sense for all the children to have the functionality?

You may want to review your design.

Thomas Matthews
  • 56,849
  • 17
  • 98
  • 154
  • No need for guarantee; that's the point of `dynamic_cast` with RTTI: if the base pointer is not a valid child pointer, `nullptr` is returned. – YSC Dec 15 '15 at 16:00