【发布时间】:2010-08-04 02:30:22
【问题描述】:
我刚刚在使用 Strawberry Perl 获得的 Windows 版 g++ 中出现了一些奇怪的行为。它允许我省略返回语句。
我有一个成员函数,它返回一个由两个指针组成的结构,称为boundTag:
struct boundTag Box::getBound(int side) {
struct boundTag retBoundTag;
retBoundTag.box = this;
switch (side)
{
// set retBoundTag.bound based on value of "side"
}
}
这个函数给了我一些不好的输出,我发现它没有返回语句。我本来打算返回retBoundTag,但忘了实际编写返回语句。一旦我添加了return retBoundTag;,一切都很好。
但是我已经测试了这个函数并得到了正确的boundTag 输出。即使是现在,当我删除 return 语句时,g++ 也会在没有警告的情况下编译它。怎么回事?猜到返回retBoundTag吗?
【问题讨论】:
-
你应该使用
-Wall进行编译。缺少的返回语句被-Wreturn-type捕获。 -
我倾向于将有关缺少返回的警告变成错误:
-Werror=return-type。为我节省了很多时间。
标签: c++ g++ strawberry-perl