为什么我的返回类型没有意义? [英] Why is my return type meaningless?

查看:98
本文介绍了为什么我的返回类型没有意义?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 const MyClass * const 的返回类型。但是,我得到一个警告:

I am trying to use a return type of const MyClass * const. However, I get a warning:


警告:#815-D:返回类型上的类型限定符是没有意义的。

Warning: #815-D: type qualifier on return type is meaningless.

这不是有效类型吗?我想要一个不能更改的指针,并且我希望它指向的东西也不能更改。

Is this not a valid type? I want a pointer than cannot be changed, and I want the thing it points to to not be changed either.

推荐答案

指针本身具有值类型,因此使其成为常量没有任何意义。调用者函数不能限制调用者函数对返回值的作用。类似于尝试定义以下内容:

The pointer itself has value type, so it doesn't make sense to make it const. What the caller function does with the returned value can't be restricted by the called function. This is akin to trying to define something like:

const int getInt();

getInt(),在这种情况下,仅返回一个int值(而不是引用)。它进入一个寄存器,然后调用方函数接收它,并对其进行所需的操作。

getInt(), in this case, just returns an int value (not a reference). It goes to a register, then the caller function receives it and does whatever it wants with it.

这篇关于为什么我的返回类型没有意义?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆