Scott Pfiffer
Scott Pfiffer

Reputation: 17

C++ Passing an object by reference?

I have a class that represents an capture zone in a game. Inside of this class I have a vector of player's that are capturing that zone (CUtlVector is a vector class from the SDK I am using)

typedef CUtlVector<ConquestPlayer*> CappingPlayersType; (defined outside of class)
CappingPlayersType CapturePlayers;

In my GameManager class, I am trying to retrieve the list of player's capturing using this code.

void GameManager::UpdateCaptureData()
{
    // Loop Active Capture Points
    for(int i = 0; i < CaptureZones.Count(); i++)
    {
        // Get List of Active Players Capturing this Point
        CappingPlayersType *CapturePlayers = NULL;
        int CapPlayersCount = CaptureZones.Element(i)->GetPlayersCapturing(CapturePlayers);


int CTriggerCaptureZone::GetPlayersCapturing(CappingPlayersType &CapPlayers)
{
    CapPlayers = CapturePlayers;

    return CapturePlayers.Count();
}

It has been a while though and I can't remember the best way to retrieve the list from the function and store it in a local variable. IIRC I can't pass by reference since the CapturePlayers object is NULL, but I am at a loss. Any help would be much appreciated.

Upvotes: 0

Views: 199

Answers (3)

Stuart Golodetz
Stuart Golodetz

Reputation: 20616

Do you mean something like this?

const CappingPlayersType& CTriggerCaptureZone::GetPlayersCapturing() const
{
    return CapturePlayers;
}

And in the calling function:

const CappingPlayersType& CapturePlayers = CaptureZones.Element(i)->GetPlayersCapturing();
size_t CapPlayersCount = CapturePlayers.size();

I haven't looked too carefully at the rest of the code (just trying to fit in with what you have), but it seems like you might just be wanting to return the internal CUtlVector by const reference.

Upvotes: 2

Cam
Cam

Reputation: 15234

If I understand correctly, you want to pass in CapturePlayers, and have GetPlayersCapturing take care of initializing it.

You have two options:

  1. You could make CapturePlayers a pointer, and accept it as a pointer in GetPlayersCapturing. You would set it to NULL, pass it in, and GetPlayersCapturing would allocate memory for it / initialize it.

  2. You could declare CapturePlayers as a local variable like this:

    CappingPlayersType CapturePlayers;
    

    Then you could accept it as a reference in GetPlayersCapturing, which would modify it before returning the count.

Ultimately which of these you choose is up to you and it depends on more context than you've shown here. However I strongly recommend option 2, simply for the reason that you should avoid dealing with pointers whenever possible.

Upvotes: 2

SilverbackNet
SilverbackNet

Reputation: 2074

I have no idea what you're doing after the GetPlayersCapturing() call, but I think overloading one call by returning both the count AND a reference to the real data is just going to trip you up unless you want to go work with raw pointers.

Why not have one function return the count, one function return a reference, and call them one after the other? No need to pass bad data, pre-initialize, or waste further time on it.

Upvotes: 0

Related Questions