Note that there are some explanatory texts on larger screens.

plurals
  1. POC++: Identifying the type of the object
    primarykey
    data
    text
    <p>I'm curious what constructs or language features, available in both the current C++ as well as in C++11, can be used to deduce the type of an object. An example:</p> <pre><code>class Base { }; class DerivA : public Base { }; class DerivB : public Base { }; void foo(Base* obj) { // Identify if `obj` is a `DerivA` or a `DerivB` } </code></pre> <p>This is an oversimplification. It would appear that rather than having a way to identify the type, the best solution is to have overloads of the function for the two derived types and do away with the base class.</p> <p>My real use case is one where one class is not interested in the exact type of the object (ie. just needs an implementation of <code>Base</code>) and another class needs to know exactly what implementation of <code>Base</code> the first class is using.</p> <p>This happens in a component-based game entity system. The base would be an <code>EntityState</code> and its derived types are <code>StandingState</code>, <code>DeadState</code>, etc. Class <code>Entity</code> is the one that only needs a generic <code>EntityState</code> object and class <code>EntityRepresentation</code> needs to know exactly what state the entity is in to decide whether to draw the "standing" animation or the "dead" animation, or whatever.</p> <hr> <p><strong>Edit:</strong> Of course, if possible, I'd like to implement the game in such a way that not even the entity representation needs to know the type of the entity state. If there's a way to do that, then I'd use it. :) I'll look into the visitor pattern.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload