简体   繁体   English

vs2010中的功能错误

[英]the function error in vs2010

I write following in vs2010: 我在vs2010中写了以下内容:

int test() const;

it tell me the const used incorrectly.and said: 它告诉我const使用不正确。

Non member function does not allow the use of type qualifier 非成员函数不允许使用类型限定符

I wonder why,does it the problem of vs?or how to use such function in vs? 我不知道为什么,这是vs的问题?还是在vs中如何使用这样的功能?

const when applied to a function is only applicable to non-static member functions, not free functions or static member functions. const在应用于const时仅适用于非静态成员函数,不适用于自由函数或静态成员函数。

class A
{
    void f1() const;        // OK
    static void f2() const; // Not OK
};

void f3() const; // Not OK

From the C++ standard: 从C ++标准:

  • 9.3.1 Nonstatic member functions 9.3.1非静态成员函数

A non-static member function may be declared const, volatile, or const volatile. 非静态成员函数可以声明为const,volatile或const volatile。

  • 9.4.1 Static member functions 9.4.1静态成员函数

A static member function shall not be declared const, volatile, or const volatile. 静态成员函数不得声明为const,volatile或const volatile。

const in that context means this method will not modify any member variables. const在这种情况下意味着该方法将不会修改任何成员变量。 If it is not a method of a class (ie it is a free floating function), then it has no meaning. 如果它不是类的方法(即它是一个自由浮动的函数),则它没有任何意义。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM