Static member variables and static member functions of the c++@ class

Source: Internet
Author: User

Reference:

http://blog.csdn.net/morewindows/article/details/6721430

Http://www.cnblogs.com/lzjsky/archive/2011/01/24/1943199.html

When you analyze the QT program, you encounter the following code.

QString str = QString ("Qframe#avatar{border-image:url (%1.jpg)}"). Arg (Qstring::number (i));
Static member variables Static member functions
Concept definition

Variables that are shared by objects of all classes.

That is, this variable belongs to this class, and it is stored only for use by all objects.

Does not access any data for an object, but is a member function of an object, it is declared as a static member function.
Form Embodiment When you define a static member variable in a class, the front plus static When you define a static member function in a class, the top-most static
Calling methods < class name >::< static member name >

< class name >::< static member name > (more common)

< class object name >.< static member function > (less used)

Grammar rules

A global variable in a class field whose definition cannot be placed in a header file, in case of a duplicate definition.

is shared by objects of all classes, including objects of derived classes.

A static member variable of a class must first be initialized and then used.

Static member functions can be called using the < class name >::< static member name > method, but ordinary member functions cannot be called this way.

Non-static members cannot be used in static member functions of a class, because static member functions do not contain the this pointer.

However, you can use static members in non-static member functions of a class.

The address of a static member function can be stored using a normal function pointer, whereas a normal member function address needs to be stored with a class member function pointer.

Static member functions cannot be declared as virtual, const, or volatile functions at the same time.

The type of a static data member can be the type of the owning class, while a normal data member cannot. A normal data member can only be declared as a pointer or reference to the owning class type.

A static member can be an optional parameter to a member function, and a normal data member may not.

Static members can be accessed independently, that is, without creating any object instances.

Class Point  {public  :       void init ()      {        }      static void output ()      {      }  };  void Main ()  {      point::init ();      Point::output ();  }  Compile Error: Error C2352: ' point::init ': illegal call of Non-static member function//the above main () can be changed to void Main ()  {      Point pt;      Pt.init ();      Pt.output ();  }  Compiled by

  

#include <stdio.h>  class point  {public  :       void init ()      {        }      static void Output ()      {          printf ("%d\n", m_x);      }  Private:      int m_x;  };  void Main ()  {point      pt;      Pt.output ();  }  Compilation error: Error C2597:illegal reference to data member ' point::m_x ' in a static member function

  

Class Point  {public  :       void init ()      {            output ();      }      static void output ()      {      }  };  void Main ()  {point      pt;      Pt.output ();  }  Compiled by

  

#include <stdio.h>  class point  {public  : Point       ()      {            m_npointcount++;      }      ~point ()      {          m_npointcount--;      }      static void output ()      {          printf ("%d\n", M_npointcount);      }  Private:      static int m_npointcount;  };  void Main ()  {point      pt;      Pt.output ();  }  Compile pass, run error. Correction: Add int point::m_npointcount = 0 before the main () function;

  

  

  

Static member variables and static member functions of the [email protected] class

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.