Download

Can't return a const pointer?

I have a function like this.


UFUNCTION(BlueprintPure, Category = "Misc")
static const AMyActor* GetByID(const int32 ID);

But it gives me the following error when I compile.


error C2440: '=': cannot convert from 'const AMyActor *' to 'AMyActor *'

If I remove the const, or remove the UFUNCTION, then it complies fine. So it must be Blueprint that doesn’t allow const pointers. Is that right? Any way around this?

Not to 100% sure what you mean, but just in case, the way you are defining it, the constness is on the MyActor and not the pointer.

This is how to return a const pointer rather than a pointer to a const type.

static AMyActor* const GetByID(const int32 ID);

It seems to compile that way (if in fact that that is what you want).

In order to return a pointer to a const object your function needs to be const as well.

“In order to return a pointer to a const object your function needs to be const as well.”
Can’t have a static const function ma friend… so not sure what your suggestion is. Also I don’t believe that is true in any case?

Having the same issue, looks like no one cares? Makes it very difficult to use config/settings objects in blueprint.

I don’t think you can pass a const ptr to blueprints, they don’t have any mechanics to handle those.