是否所有的一类属性常量需要有成员函数声明为const呢?

编程入门 行业动态 更新时间:2024-10-22 21:32:23
本文介绍了是否所有的一类属性常量需要有成员函数声明为const呢?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

标题已经说明了一切。让我扩大一点不过:我类,其所有属性常量:

The title already says all. Let me expand a little nevertheless: I've class whose all attributes are const:

template< class perm = Perm16 > class PermutationGroup { public: using StrongGeneratingSet = std::vector< std::vector< perm > >; const std::string name; const uint64_t N; const StrongGeneratingSet sgs; PermutationGroup(std::string name, uint64_t N, StrongGeneratingSet sgs) : name(name), N(N), sgs(sgs) { assert(check_sgs()); }; bool check_sgs() const; // defined as const bool is_canonical(vect v) const; // defined as const [...] };

这是任何使用定义所有成员函数作为常量为好?还是我不必要地重复自己?

Is it of any use to define all member function as const as well ? Or am I needlessly repeating myself ?

推荐答案

如果你不申报的成员函数的常量的,你不能调用他们的常量的通过的 const引用引用的对象或对象的。例如,以下将不起作用:

If you do not declare the member functions as const, you can't invoke them on const objects or objects referenced by a const reference. For example, the following won't work:

const PermutationGroup& group = PermutationGroup("foobar", 42, ...); group.check_sgs(); // ERROR: can't invoke non-const function on const objects

更多推荐

是否所有的一类属性常量需要有成员函数声明为const呢?

本文发布于:2023-11-11 07:47:54,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1577793.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:常量   函数   属性   声明   成员

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!