Can you use TObjectPtr in a covariant return type scenario?

Just started to learn about covariance in general. Don’t really understand it.

Using TObjectPtr I get:

InAirState.h(18): [C2555] 'UInAirState::UpdateCharacter': 
overriding virtual function return type differs and is not covariant from 'UBaseState::UpdateCharacter'

Using regular Raw pointers (*) , I don’t get any errors.

Something to do with TObjectPtr being a template? Which is not Covariant in C++? How would I use TObjectPtr in a covariant return type scenario?

No, that won’t work. The C++ compiler doesn’t/can’t understand how the template parameter is used to know that the two templated classes are related in a way to support covariance. I’m pretty sure it doesn’t work with smart pointers either, even they have constructors that allow them to function as if were raw pointers for assignment purposes.

If you want covariant returns you’ll need to return the raw pointer. But you should be able assign the return to a TObjectPtr if you really want. I’m not sure that template really gets you anything at runtime though. I haven’t dug too far into UE5 yet, but I thought that template really only mattered for properties. So if you’re just using it as a runtime value you may be able to just drop it’s use altogether.


Yes, I went down a bit of a rabbit hole :smiley: …, and ultimately decided to just forget the idea of using TObjectPtr in this case. Thanks for your reply!