0

I am trying to create a class structure that works like this:

class Base 
{
public:
    class NestedBase
    {
    };

    virtual std::vector<NestedBase*> getNested();
};


class Derived  : public Base
{
public:
    class NestedDerived  : public Base::NestedBase
    {
    };

    std::vector<NestedDerived*> getNested() override;
};

Where I can access the specific NestedDerived* list through an instance of Derived.

I understand why this will not work from the following posts: 1, 2.

The main alternative strategy described is to continue to use the NestedBase* vector and use dynamic_cast on every instance when using the derived class.

This solution seems messy and problematic to me since it relies on the client knowing which type the nested instances must be cast to.

Another solution which was presented was to make base into a class template that takes the NestedClass. I cannot use this since I need to use base instances without knowing their derived type.

Is there a solution available which allows me to get the specific nested list from derived classes but also allows me to get the base nested list from the base class.

In response to the comments asking for more detail: I want to use the nested class polymorphically. But I also want to use it as a specific type. The derived nested type has the common features of the base nested type with some extended functionality on top.

While dealing with the nested list in the base class, a const getter could be used which would stop other types being added, if that helps at all?

WalleyM
  • 172
  • 7
  • 1
    i dont understand the design (which is most likely not your fault). Either you do want to use it polymorphically then the caller doesn't need to know the type and returning `NestedBase*` is fine. Or no polymorphism and the caller cares about the type, then you need not override – 463035818_is_not_an_ai Apr 05 '22 at 14:25
  • Why not also give `NestedBase` virtual functions so you don't need `dynamic_cast`? – NathanOliver Apr 05 '22 at 14:27
  • Agree with the other comments, can you give us more context on what you're trying to achieve ? "Is there a solution available which allows me to get the specific nested list from derived classes but also allows me to get the base nested list from the base class." As in, getting the base nested list from the base class and filtering on whether it's a (or a child) DerivedClass using `dynamic_cast` ? If you extract it in a function, it's not messy, but something sounds off about having to do this – Julien BERNARD Apr 05 '22 at 14:37
  • I do want to use it polymorphically. But I also want to use it as a specific type. The derived nested types has the common features of the base nested type with some extended functionality on top. Virtual functions will help a bit but not to the extent that I require. – WalleyM Apr 05 '22 at 15:01
  • Then possibly use a `std::variant` and the visitor pattern – NathanOliver Apr 05 '22 at 15:05
  • @NathanOliver could you go into more detail in an answer? – WalleyM Apr 05 '22 at 15:08
  • _Virtual functions will help a bit but not to the extent that I require_ I think you need to justify that comment. Just what is it that you cannot do that way? – Paul Sanders Apr 05 '22 at 15:31
  • @PaulSanders sure. The NestedDerived class has specific data members that require additional functions to access. Perhaps, due to this, a different design is required altogether? – WalleyM Apr 05 '22 at 15:41
  • Can you not provide virtual functions in the base class to access these? You should encapsulate them in some way or another, in any case. Are there a lot of them? Are you worried about efficiency? – Paul Sanders Apr 05 '22 at 16:02
  • @PaulSanders Sorry I'm not sure I understand. The derived data members are encapsulated by the derived class. They are not relevant to the base class. – WalleyM Apr 05 '22 at 16:18
  • OK, perhaps provide a more realistic example (add it to your question) that demonstrates some of the issues you're facing, I don't really understand what you're trying to do. – Paul Sanders Apr 05 '22 at 17:15

0 Answers0