1

I have a tree which is used to store values of different types, e.g. int, double, string or whatever. There is a Base class which implements the basic tree functionality (children, parents) and a derived class template<class T> class Derived: public Base, where T is the type of value to be stored. It has a get function to return the parameters (a template function returning the type T)

I am doing a user-interface with Qt and thus need to implement a QAbstractItemModel in order to view my tree. When subclassing QAbstractItemModel I need to be able to call the get function of my Derived class, but I only have a pointer to an instance of the class (which I get from the QAbstractItemModel), and don't know its type, therefore I could cast the pointer to Base but this does not allow me to call the get function. Is there a way to find the type of my Derived class form a Base class pointer?

JeJo
  • 30,635
  • 6
  • 49
  • 88
maxwell
  • 357
  • 4
  • 14
  • 7
    Looks like [XY problem](https://mywiki.wooledge.org/XyProblem), please attach some code sample and an explaination of your actual goal. – Denis Sheremet May 23 '18 at 10:48
  • No, for all purposes each different instantiation of `Derived` is a different type. As suggested, maybe giving a bit more of context to your problem can help finding a solution. – jdehesa May 23 '18 at 10:50
  • 1
    I'm not experienced with Qt, but I guess all the items will be presented in a similar way, e.g. as a string or something like that. Maybe you could have a pure virtual method in `Base` called something like `getAsString` (or whatever you need) that returns a common representation of the value, then each subclass can implement it... – jdehesa May 23 '18 at 11:19
  • What you seem to want is similar to `boost::variant`, `QVariant` or `std::variant` with ability to learn underlying type. Regarding boost, this answer: https://stackoverflow.com/questions/8344080/boost-variant-how-to-get-currently-held-type?utm_medium=organic&utm_source=google_rich_qa&utm_campaign=google_rich_qa suggests you gotta cast and branch yourself. `QVariant` has a `type()` function based on stored type index: https://code.qt.io/cgit/qt/qtbase.git/tree/src/corelib/kernel/qvariant.cpp#n2013. `std::variant` throws on invalid get: http://en.cppreference.com/w/cpp/utility/variant – hauron May 23 '18 at 11:20
  • Yes a QVariant would work. Also boost::variant. However, ideally I would have liked to keep dependencies in the backend to a minimum. So this means there is no standard c++ way of doing this? – maxwell May 23 '18 at 11:38
  • would http://en.cppreference.com/w/cpp/language/typeid be of help? – maxwell May 23 '18 at 11:50
  • 1
    In most cases - what you need from any `Derived` add to `Base` interface (i.e. virtual method). This is wild guessing - because you did not describe your real problem - you described only problem that you have when you wanted to apply your solution to real problem - this basically means XY problem mentioned in very first comment – PiotrNycz May 24 '18 at 15:22
  • I mean - describe why you need to call ` to call the get function of my Derived class` - what it is for? – PiotrNycz May 24 '18 at 15:24

1 Answers1

-1

Your Tree does not store value of different type, it stores value of the same type (int, double, carottes, cabages) depending of the template instantiation. This is very different from having a Tree of QVariant or of any proxyable object to polymorphic class.

Template spreads along the usage until actual instantiation. You need to create a template class of T deriving from QAbstractItemModel that you will instantiate related to the type of T.

template <typename T> class MyCustomItemModel : public QAbstractItemModel;

//...
Tree<Cabagge> MyCabaggeTree;
MyCustomModel<Cabagge> MyCabaggeModel; 
sandwood
  • 2,038
  • 20
  • 38