C ++ primer Reading Notes (10)

Source: Internet
Author: User
Static class member usage  # Include <iostream> using namespace STD; class base {public: Base (int I): B (I) {} static int get_a () {return a;} PRIVATE: int B; static int A; // static const int A = 5 ;}; int base: A = 5; // int base: B = 6; // errorint main () {base (1); // cout <base: A <Endl; cout <base: get_a () <Endl; // cout <base: B <Endl;}/* usage of static variable of a class. Generally, non-static data members exist in each object of the class type, unlike common data members, static data is A member exists independently of any object of the class. Each static data member is an object associated with the class. It is not associated with objects of the class. Just as the class can define shared static data members, the class can also define static member functions. The static member function does not have this parameter, you can also directly access static members of the class, but you cannot directly use non-static members to use static members of the class. You can use the scope operator to directly call static members from the class, or use objects, indirect call by referencing or pointing to a pointer to an object of this type.

Static member functions

When we define static members outside the class, we do not need to repeatedly define static reserved words. This reserved word only appears in the Declaration within the class definition body.

The static function does not have the this pointer.

The static member is a component of the class, but not a component of any object. Therefore, the static member function does not have the this pointer.

Because static members are not part of any object, static member functions cannot be declared as const, and static member functions cannot be declared as virtual functions.

Static Data Member

Static data members can be declared as any type, including constants, references, arrays, and classes.

Static data members must be defined externally (exactly once) in the class definition body. Unlike normal data members, static members are not initialized through class constructors, but should be initialized during definition.

Note: The static keyword can only be used in the declaration of the class definition body, and the definition mark is static.

Special integer const static member

Generally, static members of a class, like normal data members, cannot be initialized in the class definition body. On the contrary, static data members are usually initialized during definition.

The exception is that as long as the initialization formula is a constant expression, the integer const static data member can be initialized in the class definition body.

Example:

Static const int period = 30; // OK

Double daily_tbl [period]; // OK, period is const variable

Note: When a const static data member is initialized in the class definition body, the data member must still be defined outside the class definition body.

When class initialization is provided, the member definition does not need to specify the initial value.

Const int account: period;

Static members are not part of class objects.

Class base

{

Public:

Base * clear (char c = item );

PRIVATE:

Static const char item = '#';}; PS: the scattered notes are returned from the const member function * This
In a common non-const member function, the type of this is a const pointer to the class type, which can change the value pointed to by this, but cannot change the address saved by this. In a const member function, this is a const pointer to a const class object. You cannot change the object that this points to or the address that this stores.
Note: normal references to class objects cannot be returned from the const member function. The const member function can only return * this as a const reference
Class member initialization:
For classes with default constructor, class members can be displayed and initialized. However, some members must be initialized in the initialization list of the constructor. For such members, assigning values to them in the constructor body does not work. Members of the class type without the default constructor, and members of the const or reference type must be initialized in the initialization list of the constructor.
Example:
Class constref
{
Public:
Constref (int ii );
PRIVATE:
Int I;
Const int ci;
Int & Ri;
};
Constref: constref (int ii)
{
I = II;
Ci = II;
Ri = II;
}
PS: You can initialize a const object or an object of the reference type, but cannot assign values to it. Initialization must be completed before the constructor is executed. The only opportunity to initialize a const or referenced type member is the reconstructor's initialization list.
Constref: constref (int ii): I (ii), CI (II), RI (ii ){}
The initialization sequence of the members is the same as the defined sequence. Do not use the members to initialize other members. Try to use the parameters of the constructor to initialize the members.

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.