为什么函数的返回类型与实际句子不一致?

Why is the return type of the function not consistent with the actual sentence?

本文关键字:句子 不一致 函数 返回类型 为什么      更新时间:2023-10-16

在程序中,它具有:

EnergyFunctional* ef;
ef->insertFrame(fh, &Hcalib);

该函数定义如下:

EFFrame* EnergyFunctional::insertFrame(FrameHessian* fh, CalibHessian* Hcalib)
{
EFFrame* eff = new EFFrame(fh);
eff->idx = frames.size();
frames.push_back(eff);
nFrames++;
fh->efFrame = eff;
assert(HM.cols() == 8*nFrames+CPARS-8); ///  CPARS = 4
bM.conservativeResize(8*nFrames+CPARS);
HM.conservativeResize(8*nFrames+CPARS,8*nFrames+CPARS);
bM.tail<8>().setZero();
HM.rightCols<8>().setZero();
HM.bottomRows<8>().setZero();
EFIndicesValid = false;
EFAdjointsValid=false;
EFDeltaValid=false;
setAdjointsF(Hcalib);
makeIDX();

for(EFFrame* fh2 : frames)
{
connectivityMap[(((uint64_t)eff->frameID) << 32) + ((uint64_t)fh2->frameID)] = Eigen::Vector2i(0,0); /// move left 32 bits
if(fh2 != eff)
connectivityMap[(((uint64_t)fh2->frameID) << 32) + ((uint64_t)eff->frameID)] = Eigen::Vector2i(0,0);  /// index is totally 64 bits, front 32 bits for each frame, rear 32 bits for current frame
}
return eff;
}

似乎返回类型应该是无效的,以便它可以与最后一句保持一致......

我还检查了是否有其他同名的"inserFrame"函数,但没有。

这个程序可以编译成功,但是为什么可以没有问题呢?

函数指定的返回类型是EFFrame*,它是指向EFFrame对象的指针。函数返回的值是eff,这也是一个EFFrame*类型的指针。所以不应该有任何混淆。

如果不返回值,则仅在函数声明中使用void返回类型。但是,如最后一行所示,此函数显然试图返回指向EFFrame对象的指针。

但是,即使在具有明确定义的返回类型的函数中,也可能不需要使用该值。因此,从技术上讲,只调用函数而不将返回值存储到变量中是合法的。因此,即使ef->insertFrame(fh, &Hcalib);调用函数并返回EFFrame*指针,它也不会被存储。