本文介绍了C ++:为什么不能将静态函数声明为const或volatile或const volatile的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

$想知道静态成员函数不能声明为const或volatile或const volatile的原因吗?

Was curious to know the reason why static member functions cant be declared as const or volatile or const volatile ?

#include<iostream>

class Test
{
   static void fun() const
   { // compiler error
       return;
   }
};


推荐答案

因为这是标准说的:

这样做的原因是 const volatile virtual (在传统意义上,见下文)。例如, const 意味着你不能修改对象的成员,但是在静态的情况下,没有对象可以谈论。

The reason for this is that a const (or volatile or virtual) static method wouldn't make sense (in the traditional sense, see below). For example, const implies you can't modify the object's members, but in the case of statics, there's no object to talk about.

您可以认为 const static 可以应用于其他 static 成员,但此选项被视为无意义。

You could argue that a const static could apply to other static members, but this option was regarded as pointless.

这篇关于C ++:为什么不能将静态函数声明为const或volatile或const volatile的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

05-28 01:29
查看更多