Does a class with all attributes const need to have member function declared const as well?

限于喜欢 提交于 2020-02-04 00:13:38

问题


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 ?


回答1:


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



回答2:


Declaring your methods as const means that you can call them on a const-qualified object. It's not necessary to declare them const just because there are no mutable fields in the class, but in general, it's easier to write const-correct code if you always declare methods that don't modify anything as const.




回答3:


Does a class with all attributes const need to have member function declared const as well?

Declaring a member variable const is different than declaring a member function const. When a member variable is declared const it cannot be modified. It can still be accessed by member functions even those not declared const, they just can't be modified.

Decalring a member function const is saying this function does not modify this object. The function is not allowed to change any member variable unless it's declared mutable. It also cannot call member functions that are not declared const as those functions would be allowed to modify the object.

Declaring member functions as const is important as it allows those functions to be called on constant instances of the class they belong to. Nosid provides a great example in their answer.




回答4:


No it does not.

That been said, constness on behalf of the attributes implies constness on behalf of the instance, so even if a member function is non const it won't be able to behave as such (ie cause state mutation)

The other way around is not true though. For example

int myClass::example() const {
    member = 1;
}

is the same as

int myClass::example() const {
    this->member = 1;
}

and constness of the member function means constness of this pointer. This is slightly different from promising that the "raw bits" of the object's struct aren't going to change. For example, reference members can be modified by const member functions

To sum up : Declaring a member function as const becomes mandatory when it's to be used with a this pointer that is const. In your case, not declaring one as const would just be futile (and break the inspector / mutator semantics .. that being less of a deal).



来源:https://stackoverflow.com/questions/23449597/does-a-class-with-all-attributes-const-need-to-have-member-function-declared-con

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!