tmighty
tmighty

Reputation: 11399

C++ object reference instead of copy

This works fine:

udtCandidate firstCand;
firstCand=uSeqs[i].Candidates.front();

This would create a copy of the udtCandidate.

But I would need a reference to it, not a copy.

However

udtCandidate firstCand;
firstCand=&uSeqs[i].Candidates.front();

does not work. The compiler tells me that there is no binary operator "=" that accepts a right-handed operand of the type "udtCandidate *".

Does anybody know what I did wrong?

The declarations are:

struct udtCandidate
{
    udtJoinFeatures JoinFeaturesLeft;
    udtJoinFeatures JoinFeaturesRight;
    udtByteFeatures ByteFeaturesLeft;
    udtByteFeatures ByteFeaturesRight;
    int iHPUnitIDLeft;
    int iHPUnitIDRight;
    double targetCost;
    vector<unsigned long>bestPath;
    double bestPathScore;
    bool hasAncestor;
};
struct udtCandidateSequence
{
    double Score;
    vector<udtCandidate>Candidates;
};

Upvotes: 0

Views: 87

Answers (1)

Bartek Banachewicz
Bartek Banachewicz

Reputation: 39390

To store a reference instead of a value, you have to create a reference variable:

udtCandidate& firstCand = uSeqs[i].Candidates.front();

Using & as you did means address-of operator, which in turn changes the type to a pointer.

Upvotes: 1

Related Questions