本文介绍了是否所有的一类属性常量需要有成员函数声明为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呢?
发布评论