C + + cannot specify a keyword static outside the class body

Source: Internet
Author: User

problems with the C + + static function

Recently read C + + primer in the static chapter, there is such a sentence,

"The declaration of a static member function, in addition to the function declaration in the class body, is preceded by a keyword static and cannot be declared as
A function definition other than const or volatile that appears in the same class body as a non-static member function cannot specify a keyword
Static

Why can't I specify the keyword static outside the class body, so what is the purpose of the design and what is the use?

*

How to say, this is a scope of the problem!
The scope of a member function is a class field, and adding static outside the class body does not represent a static function, it means that the function owns the file domain
And the class domain is smaller than the file domain, forcing the class domain to be extended to the file domain, there will be an error.
The following code:
Class CA {
Public
static void display (void);
};

static void CA::d isplay (void) {//error!
cout << "Hello ca!" << Endl;
}

int main (int argc, char* argv[]) {
CA::d isplay ();
}

Error C2724: ' CA::d isplay ': ' Static ' should not being used on member functions defined at file scope

C + + cannot specify a keyword static outside the class body

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.