C + + static member functions
Like a data member, a member function can also be defined as static, and the preceding addition of a function in a class becomes a static member function. Such as
static int volume ();
As with static
1 General class static member functions cannot call non-static member functions2 can a static member function call a constructor?The answer is yes, because the static member function does not have this pointer, the general static member function
A static member function is a member function of a class that does not belong to any object of the class request, but is a function shared by all members of that class.For example, you have 10 people in your class (10 for Class one by one), and the
Static member functionsAs with static data members, we can also create a static member function that serves the entire service of a class rather than the specific object of a class. Static member functions, like static data members, are internal
1. Destructors1. Destructors have no arguments and cannot be overloaded, so a class can have only one destructor. If the user does not have a definition, the compiler will generate it automatically. 2. static The local object is not destroyed at the
1. static member functions(1) A static member function is a special member function in a class that belongs to the entire class.(2) public static member functions can be accessed directly through the class name(3) public static member functions can
In this regard, let me begin by saying something about:The callback function must be a static member function or a global function to implement a callback function, presumably because a normal C + + member function implies a function parameter, the
First, static each static A data member is an object associated with a class and is not associated with an object of that class ! The non-static data member exists in each object of the class type, and the static data member is
C ++ Primer study note _ 22 _ class and data abstraction (8) -- static member variable, static member function, class/object size, _ 22 -- static
I. static
Each static data member is an object associated with the class and is not associated with the
Before I turn to the point, I think we should first put forward two of my own custom terms: class level members and object level members. I don't know if there is such a term in the C + + domain, but I think these two terms can help us understand
As with static data members, static member functions are shared by all objects, not individually, because static member functions do not pass the this pointer, so static member function can only access static members and cannot access non-static
Recently see effective C + + often see some and static related content, hope that a comprehensive collation, if not all, also hope to add:Static member variables in Class 1Static members Unlike normal data members, static data members exist
In the implementation of member functions, non-static members described in the class cannot be directly referenced. Static members described in the class can be referenced, if a non-static member is to be referenced in a C ++ static member function,
I. static member variables
For all objects of a specific type, you may need to access a global variable. For example, count the number of created objects of a certain type.If we use global variables, the Data encapsulation will be broken. Generally,
A static member cannot be assigned in the class body because it is shared by all objects of that class. You assign a value to it in an object, and that member of the other object
will also change. To avoid confusion, it is not necessary to assign a
Why cannot an inline function, constructor, or static member function be a virtual function?
1> inline functions
Inline functions are expanded during compilation, and virtual functions are dynamically integrated during runtime. Therefore, they
In fact, we can intuitively understand that static member functions cannot invoke non-static member variables.Because both static member functions and static member variables are in the category of classes, andThere is always only one copy of the
Static member functions cannot be modified with const or access to nonstatic dataThe reason that static member functions in C + + cannot be modified with const:The fifth meaning of static in C + + is that class member functions that do not access
When a data member of a class is declared static, the static data member can only be defined once, and is shared by all objects of the same kind. Each object has a copy of every regular data member in the class, but only one instance of the static
the static member function of the class brings what benefit and should be used at what time. Tags: math design pattern Algorithm API Programming store 2011-10-13 18:15 3382 person reading reviews (0) Favorite report Category: C/C + + programming (7)
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.