0

Currently I have class A, with derived types, B and C.

class A {};
class B : public A {};
class C : public A {};

I then have a vector of type A, which I append a single object of types B and C. Like so:

std::vector<A*> list = new std::vector<A*>;
list->push_back(new B);
list->push_back(new C);

My main question is: How would I go about getting an object of a specific type (that inherits from A), from this vector?

I have attempted to use templates, though this gives unresolved external symbol error

template <class T>
T* GetObject()
{
    for (A* object : list)
    {
        if (typeid(*object) == typeid(T)) return object;
    }
    throw std::exception("vector does not have object of that type");
}
Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
Tom Martin
  • 300
  • 1
  • 2
  • 12
  • See https://stackoverflow.com/q/2253168/4658169 – CuriouslyRecurringThoughts Jun 20 '19 at 07:08
  • 3
    You could use `dynamic_cast` and check for nullptr. But this seems like an XY problem. What are you trying to achieve? – Neijwiert Jun 20 '19 at 07:09
  • 1
    The need to do that is very often a sign that the design could be improved. – molbdnilo Jun 20 '19 at 07:13
  • 1
    we need an [mre] but presumably your unresolved symbol is due to https://stackoverflow.com/questions/495021/why-can-templates-only-be-implemented-in-the-header-file – Alan Birtles Jun 20 '19 at 07:15
  • 1
    My crystal ball thinks that you should read [Why can templates only be implemented in the header file?](https://stackoverflow.com/questions/495021/why-can-templates-only-be-implemented-in-the-header-file). – molbdnilo Jun 20 '19 at 07:19

1 Answers1

2

Give A a virtual method, such as the destructor (which you should do anyway when writing polymorphic types), and then you can use dynamic_cast:

class A {
public:
    virtual ~A() {}
};

class B : public A {};
class C : public A {};

...

std::vector<std::unique_ptr<A>> list;
list.push_back(std::unique_ptr<A>(new B));
list.push_back(std::unique_ptr<A>(new C));

...

template <class T>
T& GetObject()
{
    for (auto &object : list)
    {
        T *p = dynamic_cast<T*>(object.get());
        if (p)
            return *p;
    }
    throw std::exception("vector does not have object of that type");
}
Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
  • This is essentially what I've been implementing for the last hour or so! – Tom Martin Jun 20 '19 at 08:37
  • Regarding the return type of GetObject, should it be T& or T*, both appear to work. – Tom Martin Jun 20 '19 at 08:38
  • @TomMartin both will work, but since the function can never return a null pointer, a reference may make more sense. It really depends on how the return value will be used by the caller – Remy Lebeau Jun 20 '19 at 14:55