C + + static variables and static member functions of the strange temper

Source: Internet
Author: User
Tags sendmsg

today, for the sake of brevity of the code, a class Msghelper is used to define static member variables and static member functions.

At the beginning, my writing is (not real code, just an example):

<strong>class helper{static int index;static void sendmsg (Tcpsocket *&,string &);};static int helper:: index=1;static void Helper::sendmsg (Tcpsocket * &sock,string &msg) {if (!sock | | Msg.length () ==0) return;sock->write (MSG); return;} </strong>

Even compile the error, and later corrected to:

<strong>class helper{static int index;static void sendmsg (Tcpsocket *&,string &);}; int helper::index=1; void Helper::sendmsg (Tcpsocket * &sock,string &msg) {if (!sock | | Msg.length () ==0) return;sock->write (MSG); return;} </strong>

That is, static member functions or variables are defined outside the class body to remove the static identifier;

Note: You can initialize in a class only if the static member variable type in the class is normally shaped, such as:

Class simple_string{static const int length=1;static char str[length + 1];}; Char simple_string::str[length +1]= "Default string";

Other types, even if declared as const types, cannot be initialized in a static class.




C + + static variables and static member functions of the strange temper

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.