gpt4 book ai didi

C++ 对象引用而不是复制

转载 作者:行者123 更新时间:2023-11-28 07:24:47 25 4
gpt4 key购买 nike

这很好用:

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++ 对象引用而不是复制,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18932010/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com