1

I've got the following classes:

class Father {
//some code
}

class child1: public Father {
//some code
}

class childN: public Father {
//some code
}

I've got two list of Father references. I need to check if one instance of a list is present in the other list. I need to do a "deep" check (not a pointer comparison). I can do it doing a dynamic_cast and then comparing the objects, but I'm wondering if there is some smart approach or pattern.

Edit: The Father class is a generic class and an unique identifier can be of some meaning only for some child classes.

greywolf82
  • 21,813
  • 18
  • 54
  • 108
  • Give them a unique id (e.g. a name), then you only have to compare this – 463035818_is_not_an_ai Mar 06 '15 at 16:56
  • Do you need to check if an *instance* of the object is in the list? Or an object which logically evaluates to equivalent? – Neil Kirk Mar 06 '15 at 16:57
  • +tobl303 Yep, I need to update my question but I'd like to avoid id in the father class because an id can be "meaning" only in child1 for example – greywolf82 Mar 06 '15 at 16:57
  • btw are you sure about your classes? Any child is a father?? – 463035818_is_not_an_ai Mar 06 '15 at 16:57
  • @NeilKirk I need a *logical* comparison. – greywolf82 Mar 06 '15 at 16:58
  • Is `typeid` an option, or are you trying to avoid rtti? – imreal Mar 06 '15 at 16:59
  • 2
    You may look at [Double_dispatch](http://en.wikipedia.org/wiki/Double_dispatch) – Jarod42 Mar 06 '15 at 16:59
  • +imreal I'd like to avoid rtti – greywolf82 Mar 06 '15 at 17:00
  • 2
    `dynamic_cast` _is_ the smart approach, trying to create your own version of that with IDs in types will generally be inferior. The general problem is known as [multiple dispatch](https://en.wikipedia.org/wiki/Multiple_dispatch#C.2B.2B) – Jonathan Wakely Mar 06 '15 at 17:01
  • @JonathanWakely The multiple dispatch seems the right way or at least is the right "pattern". Thanks. – greywolf82 Mar 06 '15 at 17:02
  • The problem with `dynamic_cast` is performance, and the fact that if you do `dynamic_cast(obj)` and the runtime type is `childX` you will get a cast, so you have to go bottom-up on the hierarchy. On the other hand `typeid` is harmless. – imreal Mar 06 '15 at 17:02
  • @imreal Father is abstract – greywolf82 Mar 06 '15 at 17:03
  • What about adding something like `virtual std::string hash()` and comparing hashes? – Kane Mar 06 '15 at 17:05
  • @Kane It can be a good Java-style approach :) – greywolf82 Mar 06 '15 at 17:06
  • @imreal, performance can be slow for complex hierarchies. In this case, not so much. I don't understand the rest of your comment. what is the static type of `obj` in your example? What does going bottom-up in the hierarchy mean? If `Father` is the base of the whole hierarchy why would you ever `dynamic_cast` unless you have `void*`, which is not a problem in this case? – Jonathan Wakely Mar 06 '15 at 18:15
  • @JonathanWakely You're right about performance but I don't like to make assumptions about how big a hierarchy can become. Same thing for the other part of the comment, the hierarchy can grow to have multiple levels and could lead to incorrectly dispatching the method to the parent's implementation. So you have to go bottom-up (possibly in an `if...else` chain), i.e. tesing `dynamic_cast` before `dynamic_cast`. – imreal Mar 06 '15 at 19:16

0 Answers0