这很好用:

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


这将创建udtCandidate的副本。

但是我需要引用它,而不是副本。

然而

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


不起作用。
编译器告诉我,没有二进制运算符“ =”可以接受类型为“ udtCandidate *”的右手操作数。

有人知道我做错了吗?

声明是:

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;
};

最佳答案

要存储引用而不是值,您必须创建一个引用变量:

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


像以前一样使用&表示地址运算符,它依次将类型更改为指针。

关于c++ - C++对象引用而不是拷贝,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/18932010/

10-13 04:55