C++中的静态可变成员变量? [英] static mutable member variables in C++?

查看:95
本文介绍了C++中的静态可变成员变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么或出于什么原因不能将 C++ 中的类成员变量声明为 static mutable?类似的东西

why or for what reason is it not possible to declare a class member variable in C++ as static mutable? Something like

static mutable int t; //This won't compile

对我来说,没有理由禁止此类声明.例如.出于维护全局类范围统计的原因,拥有可以通过(逻辑上)const 方法更改的静态变量可能很方便.因此,这要么是 C++ 中的某种错误设计并且不必要地复杂化,要么存在我看不到的实际或理论原因.

For me, there is no reason to ban such declarations. E.g. for reasons like maintaining a global class-wide statistics, it may be convenient to have static variable that can be altered by (logically) const methods. So either this is sort of a misdesign in C++ and unnecessarily complicated, or there is a practical or theoretical reason which I cannot see.

推荐答案

类的非常量静态成员已经可以被类的任何(常量和非常量)方法修改.没有必要也没有意义用 mutable 声明它.那绝对不会有任何成就.

Non-const static members of the class can already be modified by any (const and non-const) methods of the class. There's no need and no point in declaring it with mutable. That would achieve absolutely nothing.

这篇关于C++中的静态可变成员变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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