const正确性:const char const * const GetName const(// stuff); [英] Const correctness: const char const * const GetName const (//stuff);

查看:107
本文介绍了const正确性:const char const * const GetName const(// stuff);的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

标签为家庭作业,因为这是一个中期问题,我写道,我不明白答案。我被要求在以下语句中解释每个const的目的:

Labelled as homework because this was a question on a midterm I wrote that I don't understand the answer to. I was asked to explain the purpose of each const in the following statement:

const char const * const GetName() const { return m_name; };

那么,每个const的解释是什么?

So, what is the explanation for each of these consts?

推荐答案

从右边取。在; 之前的一个告诉客户端这是一个设计级const,即它不会改变对象的状态。 (将此视为只读方法。)

Take them from the right. The one before the ; tells the client this is a design level const i.e. it does not alter the state of the object. (Think of this as a read-only method.)

好,现在返回值:

const char const *const

这是一个常量指针。在这里我们去繁荣!您有一个额外的 const - 语法错误。以下是等价的: const T T const 。如果你取出一个 const ,你会得到一个常量指针指向一个常量字符。这有帮助吗?

This is a constant pointer to okay ... here we go boom! You have an extra const -- a syntax error. The following are equivalent: const T or T const. If you take out a const you get a constant pointer to a constant characters. Does that help?

这篇关于const正确性:const char const * const GetName const(// stuff);的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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