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.
36 messages · Page 1 of 1 (latest)
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 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.
```cpp
int main() {}
```
int main() {}
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
List<A> and List<B> are two completely different, unrelated types. you can't cast between them.
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.
https://learn.microsoft.com/en-us/dotnet/csharp/programming-guide/concepts/covariance-contravariance/
C# docs on covariance but should be applicable to C++/CLI.
https://learn.microsoft.com/en-us/cpp/extensions/generics-and-templates-visual-cpp?view=msvc-170
See this to know the difference between C++/CLI generics and templates.
a need to downcast is generally an indicator that something is wrong with the design.
Yes, that is something i have read a couple of times. I assume it's better to just have a list of children in the derived class?
Thanks for your detailed answer, i'll be removing this implementation tho. Thanks sfor the links i'll read them!
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
In .NET code it is quite common. Very suspecious with collections, however.
yeah, I'm aware that it's quite common in .NET. it doesn't really make it any good though ^^
just because there's polymorphism doesn't mean you should need to downcast
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.
something something CRTP
It is needed with CRTP
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.
lol you are not alone buddy
I actually had to search the term on the internet to make sure the title of this forumn post is correct.
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
No
With CRTP you get an unique base, which may be important in some cases.
I don't know the percentage though.
you can do, with deducing this, most of what CRTP was needed for without the templating though