#Downcasting from template class to derived class C++/CLI

36 messages · Page 1 of 1 (latest)

worldly pondBOT
#

When your question is answered use !solved to mark the question as resolved.

Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For tips on how to ask a good question use !howto ask.

#

@hidden python

Please Do Not Delete Posts!

Please don't delete forum posts. They can be helpful to refer to later and other members can learn from them. In the future you can use !solved to close a post and mark a post as solved.

#
How to Format Code on Discord
Markup

```cpp
int main() {}
```

Result
int main() {}
hidden python
#

I've created a "ManagedObject" template class containing a List<ManagedObject^>^ for its children. It a wrapper class that manages native C++ objects for an C++/CLI project.
I've derived from this class and named it "OCCTObject3D". Within "OCCTObject3D", there's a property that returns its children as a List<OCCTObject3D^>^.
Downcasting the list from the base class results to a nullptr.

Is something like this not possible, or how do i fix this?

public ref class ManagedObject
{

protected:
    List<ManagedObject^>^ m_Children;

    ...

};
{
public:
    property List<OCCTObject3D^>^ Children
    {
        List<OCCTObject3D^>^ get() {
            return dynamic_cast<List<OCCTObject3D^>^>(ManagedObject::m_Children)
        }
    }

}```
#

Downcasting from template class to derived class C++/CLI

scarlet stump
#

List<A> and List<B> are two completely different, unrelated types. you can't cast between them.

glacial comet
#

There is something called covariance for collections of .NET generic types.

#

These are not .NET generic types - these are C++ templates creating distinct .NET types.

#

If possible change template <class T> to generic <class T>, which will give you an actual .NET generic type.

#

Though well, it is still suspicious to do something like this with an mutable collection.

#

The elements in m_Children need not all be of type OCCTObject3D.

#

But well, changing template to generic will let you do this I think.

scarlet stump
#

a need to downcast is generally an indicator that something is wrong with the design.

hidden python
hidden python
worldly pondBOT
#

Thank you and let us know if you have any more questions!

This thread is now set to auto-hide after an hour of inactivity

glacial comet
scarlet stump
#

yeah, I'm aware that it's quite common in .NET. it doesn't really make it any good though ^^

glacial comet
#

Well .NET itself bad then.

#

The whole type system is rooted with polymorphism.

scarlet stump
#

just because there's polymorphism doesn't mean you should need to downcast

glacial comet
#

Things like these are hard to reason about in actual code though. Same with everything exhibiting reference semantics.

#

There are some cases where you put your own data inside an untyped thing but maybe should not be as common as they are.

glacial comet
#

But that is static downcast

#

Well actually with CRTP your base is unique to the derived so can be considered as the same type

#

Funnily enough, I think of downcast as upcasting. I see the base being at the bottom.

glacial comet
#

I actually had to search the term on the internet to make sure the title of this forumn post is correct.

wary grotto
# glacial comet It is needed with CRTP

yes. duh. that's my point. though i guess one could say that CRTP is itself is problematic design? now that we have reducing this, like 99% of use cases for CRTP went away

glacial comet
#

No

#

With CRTP you get an unique base, which may be important in some cases.

#

I don't know the percentage though.

wary grotto
#

you can do, with deducing this, most of what CRTP was needed for without the templating though