为什么std::复数实成员函数不是const
Why is std::complex real member function not const?
std::complex类中的成员函数"real"answers" image "不为const的原因是什么?
在complex
类模板中real
有两个重载:
T real() const;
void real(T);
上的第一个是 const
,所以这不是你要问的。
第二个方法,它接受一个T
参数,不返回任何东西,不是const
,因为它是一个"setter"方法——它的全部意义是改变对象的状态,所以最好不是const
。
让我们看看c++标准:
c++ 2011第26.4.2节类模板复合物
namespace std { template<class T> class complex { public: typedef T value_type; complex(const T& re = T(), const T& im = T()); complex(const complex&); template<class X> complex(const complex<X>&); T real() const; void real(T); T imag() const; void imag(T); complex<T>& operator= (const T&); complex<T>& operator+=(const T&); complex<T>& operator-=(const T&); complex<T>& operator*=(const T&); complex<T>& operator/=(const T&); complex& operator=(const complex&); template<class X> complex<T>& operator= (const complex<X>&); template<class X> complex<T>& operator+=(const complex<X>&); template<class X> complex<T>& operator-=(const complex<X>&); template<class X> complex<T>& operator*=(const complex<X>&); template<class X> complex<T>& operator/=(const complex<X>&); }; }
我想说的是,std::complex::real()
和std::complex::imag()
都是const方法。
相关文章:
- 如何使用指针传递给函数的数组中对象的函数成员
- c++构造函数成员初始化:传递参数
- 创建 std::函数,它返回具有函数成员值的变量.分段错误
- 如何在C++通过公共函数访问私有函数成员?
- 解释了构造函数成员初始化列表
- 调用std::函数成员时内存损坏
- 是否可以为模板类的模板函数成员设置别名?
- 捕获 lambda 函数C++成员变量
- 构造函数成员初始值设定项跨成员列出,安全吗?
- 获取与在模板参数中传递的函数成员类型相同的类
- 如何从公共函数成员访问地图私有成员
- C 构造函数成员分配优化
- 使用命名空间进行函数成员定义
- 函数成员作为 CUDA 内核的参数
- 模板基类函数成员的别名
- 函数成员中用于void和继承的enable_if
- 头文件中是否定义了一个很长的Class函数成员
- 类内/构造函数成员初始化
- 使用指向部分专用函数成员的指针自动填充向量
- 指向函数成员的指针