我是否应该声明任何可以为 const 的方法

发布于 2024-11-01 19:48:47 字数 119 浏览 1 评论 0原文

简单的问题。 我应该声明任何可以为 const 方法的方法吗? 这包括不返回任何成员变量或返回对成员变量的 const 引用的方法。是否有任何理由不这样做(除了明显的原因,编译器无论如何都会指出)?

Simple question. Should I declare any method that can be const a const method? This includes methods that don't return any member variables, or return const references to member variables. Is there any reason not to do so (apart from the obvious reasons, which the compiler will point out anyway)?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

夜夜流光相皎洁 2024-11-08 19:48:47

不能通过指向常量对象的指针调用非 const 方法。因此,如果方法可以const,那么不声明它为const就会对其使用施加人为限制。

除此之外,使方法成为 const 是一个重要的语义细节,可以让用户感受到调用它所预期的效果。

A non-const method cannot be called through a pointer to a constant object. So if method can be const, not declaring it const would impose artificial limits on its use.

Apart from that, making a method const is an important semantic detail to give the user a feeling of the effect that could be expected from calling it.

圈圈圆圆圈圈 2024-11-08 19:48:47

看来我是这里的不和谐音符:

我应该声明任何可以为 const 方法的方法吗?

不,应该在设计过程中在不同的层面上做出决定。您应该将所有语义不修改对象的方法标记为const。这可能包括一些实际上修改一些不属于对象可感知状态的内部细节的方法(并且这些属性应该是可变的),并且可能不包括有些方法根本不会改变任何东西。

enum impl {                 // different implementations of the algorithm
   one_implementation,
   another_implementation
};
class example {
   mutable std::pair<bool, int> cache;
protected:
   int a, b;
public:
   example( int a, int b ) : cache(), a(a), b(b) {}
   virtual ~example() {}
   void set( int _a, int _b ) { 
      cache.first = false;      // invalidate previous result 
      a = _a;
      b= _b;
   }
   int expensive_calculation() const {
      if ( !cache.first ) {
         cache.second = calculate();
         cache.first = true;
      }
      return cache.second;
   }
   virtual void change_impl( impl x ) {}
private:
   virtual int calculate() const = 0;
};

在当前形式中,您无法更改实现,并且 change_impl 是非常量,即使它不修改任何成员属性,它也不会标记为 const,因为 < em>语义上它确实改变了。

另一方面,expensen_calculation() 方法不会在语义上修改对象的状态,在调用该操作之前和之后可感知的状态将是相同的,但是它确实修改了 cache 属性以加快以后的调用速度(如果状态没有改变)。因此,该方法是const,而缓存是mutable

It seems that I am to be the discording note here:

Should I declare any method that can be const a const method?

No, the decision should be taken at a different level, during design. You should mark as const all methods that semantically don't modify the object. This might include some methods that do actually modify some internal detail that is not part of the perceivable state of the object (and those attributes should be mutable) and it might not include some methods that do not change anything at all.

enum impl {                 // different implementations of the algorithm
   one_implementation,
   another_implementation
};
class example {
   mutable std::pair<bool, int> cache;
protected:
   int a, b;
public:
   example( int a, int b ) : cache(), a(a), b(b) {}
   virtual ~example() {}
   void set( int _a, int _b ) { 
      cache.first = false;      // invalidate previous result 
      a = _a;
      b= _b;
   }
   int expensive_calculation() const {
      if ( !cache.first ) {
         cache.second = calculate();
         cache.first = true;
      }
      return cache.second;
   }
   virtual void change_impl( impl x ) {}
private:
   virtual int calculate() const = 0;
};

In its current form, you cannot change the implementation, and change_impl is non-const, even if it does not modify any member attribute it is not marked as const, because semantically it does change.

On the other hand, the expensive_calculation() method does not semantically modify the state of the object, the perceivable state will be the same before and after the operation is called, but it does modify a cache attribute to speed up later calls (if the state has not changed). As such, the method is const, and the cache is mutable.

逆流 2024-11-08 19:48:47

是的。根据Effective C++,“尽可能使用const”。

Yes. According to Effective C++, "use const whenever possible".

故事↓在人 2024-11-08 19:48:47

如果方法不修改对象的逻辑状态,则应将方法标记为 const。这对于对象的客户端来说是一项很棒的服务,因为给定常量引用/指针,它们就可以得到最大程度的使用。

If a method does not modify the logical state of the object, then you should mark the methods as const. This is a great service to the clients of your objects, as they can then be used to the fullest extent, given const references/pointers.

﹉夏雨初晴づ 2024-11-08 19:48:47

在一种情况下,我会三思而后行地使用 const:基类虚函数。继承的类不能通过其重写的函数进行更改可能是一件好事,但一些开发人员可能不同意,并且因此必须克服困难。

There is 1 case where I would think twice about using const: a base class virtual function. It might be a good thing that inherited classes can't be changed by their overridden function, but some developers might disagree and really have to jump through hoops because of this.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文