How to implement a getter to return a TMap, but not allowing modification and not doing any copy of it

Hi!

This is part of Game Instance Subsystem class declaration:


TMap<int32, TSharedRef<FMyStruct>> GetMyData();

private:
    TMap<int32, TSharedRef<FMyStruct>> MyData;

I want to allow read the variable MyData, but without modifying it and without making any copy. How can I do it?

Maybe, this way:

const TMap<int32, TSharedRef<FMyStruct>>& GetMyData();

Or as an output parameter.

Thanks!

Yes, you would return a const& parameter. You would also mark your function as const so that clients with a const pointer could call it. so the full signature would be:

const TMap< >& GetMyData() const;

1 Like

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.