C + + static member functions

Source: Internet
Author: User

Today review teacher yesterday talk of static member functions. Always feel this thing exists really inexplicable, degree Niang, found on the internet is very unclear. Or use Google to find the answer from the America website.

class something{private:static int s_nvalue;}; int something::s_nvalue = 1; initializer int main () {//How do we access something::s_nvalue?}

  

In this case, we can ' t access Something::s_nvalue directly from main (), because it is private. Normally we access private members through public member functions. While we could create a normal public member function to access S_nvalue, we ' d then need to instantiate an object of the C Lass type to use the function! We can do better. The the answer to the problem are that we can also make member functions static.

Like static member variables, static member functions is not attached to any particular object. Here are the above example with a static member function accessor:

class something{private:static int s_nvalue;public:static int GetValue () {return s_nvalue;}}; int something::s_nvalue = 1; initializer int main () {std::cout << something::getvalue () << Std::endl;}

  

Above from http://www.learncpp.com/cpp-tutorial/812-static-member-functions/

So, the static member functions is purely to avoid the hassle of accessing the private static variable and then calling the method to modify the value of the new object.

But I remember the teacher said that static can also modify non-static values through objects. Use the following code to test the next

#include <iostream>using namespace std;class staticfunction{public:static int y;     int Z;     static void operate (int x) {y = x;};     static void Operate (Staticfunction M, int x) {m.z= x;}}; int staticfunction::y = 0;int Main () {    staticfunction::operate (5);    cout << staticfunction::y << Endl;    staticfunction example;    Staticfunction::operate (example, 6);    cout << example.z << Endl;    Cin.get ();}

  

Results each time VS2013 is prompted for Z uninitialized. began to think that there is no copy of the structure of the reason, but also write the structure of the letter, is a new class, tossing a half-day or not solve the problem. Finally, finally--

static void Operate (Staticfunction M, int x) {m.z= x;}

  

This obviously has nothing to do with the value of example, in front of M added a twist (&), problem solving ... Tat my time ... I'm still too young.

C + + static member functions

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.