What value do you return for a function that has the return type TSharedRef&

I am overriding FChatRoomInfo and it requires to implement the function:

const TSharedRef& GetOwnerId() const = 0;

However I do not know how to return a reference to a shared reference. It keeps failing to compile with the error C4172: returning address of local variable or temporary, even though I am returning a class member variable of type TSharedRef.

Hi Hexxxal,

It sounds like you’re returning a reference to a temporary variable (like OwnerId.ToSharedRef()), where the return value is not being stored. Because the function returns a reference to the sharedref rather than an instance, you need to make sure the value has a longer lifetime. The easiest way to is have a TSharedRef member variable (not TSharedPtr) on the FChatRoomInfo and just return that.

That is what I am doing at the moment. So I have a member variable:

TSharedRef(const FSocUniqueNetId) IdRef;

Where FSocUniqueNetId is a subclass of FUniqueNetId. In the function this is what I am doing at the moment:

const TSharedRef(const FSocUniqueNetId)& FSocChatRoomInfo::GetOwnerId() const
{
return IdRef;
}

Sorry using ( for < since they get removed in the comment