Static member __c++ for C + + classes

Source: Internet
Author: User

If you want a member of a class to be able to share all of the class objects, you can use static members to implement them.
Use the example in the book to define an account class that represents the accounts in which a InterestRate member represents the interest rate. It can be known from the actual situation that all the accounts corresponding to the interest rate should be equal, so you can define the InterestRate member as a static member.
None of the objects in each class contains any data related to static data members.

definition of static members
static functions and static data members are defined in the following manner, preceded by the keyword static

Code to be used:

Class Statictest
{public
:
    static int statica;
    static int staticFun1 ();
    static const int staticconstb=0;
    int A;
Private:
    static int STATICB;
    static int StaticFun2 ();
    int b;
};

initialization of static members in a class:
Static members cannot be initialized in constructors, otherwise each object is created with a new static member, which is inconsistent with the definition of a static member phase. Initialization method for publicly-owned static data members:

int statictest::statica=0;

You can initialize directly outside the class, noting that you cannot initialize a data member at the same time as you define it.

Class Statictest
{public
:
    static int statica=1;//error.
    ...
};
Initialization of a public constant static data member
Initialized directly at the time of definition
Class Statictest
{public
:
    static const int staticconstb=0;
    ...
};

You can also use the CONSTEXPR keyword. Initialization method for private static data members:
As with the public, it is also initialized outside of the class, except that it cannot be invoked directly through the class object, but only through a member function.

int statictest::staticb=1;
int main ()
{
    Ios::sync_with_stdio (false);
    Statictest s;
    cout<<s.staticb<<endl;//the error!
    return 0;
}
How private static data members are used:
You can use the scope operator directly, object invocation, pointer invocation, and so on. Here, just note that the defined constants are private or public.
int main ()
{
    Ios::sync_with_stdio (false);
    cout<<statictest::statica<<endl;
    Statictest s;
    cout<<s.statica<<endl;
    Statictest *p=&s;
    cout<<p->statica<<endl;
    return 0;
}
How to use private static function members:
The static member function cannot be put into a non-static member first, because the static function member does not actually belong to any object, so he does not have the this pointer.
You can define a specific method of a static function member directly within a class, or you can define it outside of the class.
int statictest::staticfun1 ()
{return
    statica+1;
}

The same is true for private static member functions.

Similarly, you can assign an initial value to a static data member in a member static function.

static int staticFun1 ()
{return
    ;
}
int statictest::staticb=staticfun1 ();

application of static data

Because static members belong to the entire class, not to the object of a class. So a static data member can be an incomplete type .
The book has the following code:

Class Bar
{
private:
    static bar mem1;
    Bar *mem2;
    Bar mem3;//error, data member must be full type
};

A static member can also be the default argument.
The Book Code:

Class screen
{public
:
    screen& Clear (char =bkground);//return a left value reference
private:
    static const char bkground;
};
const char screen::bkground= ' 0 ';

To be continue~

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.