gpt4 book ai didi

c++ - 警告 : type qualifiers ignored on function return type [-Wignored-qualifiers]

转载 作者:行者123 更新时间:2023-12-01 15:10:58 31 4
gpt4 key购买 nike

我正在尝试使用 GCC 编译器编译以下代码

class Class
{
public:

uInt16 temp;
const uInt32 function() const;

}

inline const uInt32 class::function() const
{
return temp;
}

我收到以下编译器警告

warning: type qualifiers ignored on function return type [-Wignored-qualifiers]

有什么办法可以解决这个警告吗?

最佳答案

简单使用:

uInt32 function() const;

返回 const 原始类型是没有用的,因为即使没有 const,你也无法做到 c.function()++

Returning const Object 用于模拟原语并禁止类似于上面的代码,但是现在(从 C++11 开始),如果需要,我们可以彻底禁止它:

struct S
{
S& operator ++() &;
S& operator ++() && = delete;
};

S f(); // Sufficient, no need of const S f() which forbids move

关于c++ - 警告 : type qualifiers ignored on function return type [-Wignored-qualifiers],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60222874/

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