小编Lin*_*ity的帖子

在C++中省略return语句

我刚从草莓Perl的g ++版本获得了一些奇怪的行为.它允许我省略一个return语句.

我有一个成员函数,它返回一个由两个指针组成的结构,称为boundTag:

struct boundTag Box::getBound(int side) {
    struct boundTag retBoundTag;
    retBoundTag.box = this;
    switch (side)
    {
        // set retBoundTag.bound based on value of "side"
    }
}
Run Code Online (Sandbox Code Playgroud)

这个函数给了我一些不好的输出,我发现它没有返回语句.我原本打算回来,retBoundTag但忘了真正写回报.一旦我添加return retBoundTag;一切都很好.

但我测试了这个功能,并boundTag从中获得了正确的输出.即使是现在,当我删除return语句时,g ++会在没有警告的情况下编译它.WTF?猜它会回来retBoundTag吗?

c++ g++ strawberry-perl

21
推荐指数
2
解决办法
6740
查看次数

标签 统计

c++ ×1

g++ ×1

strawberry-perl ×1