Static member initialization of C + + classes detailed explanation of _c language

Source: Internet
Author: User
Tags class definition constant function definition

Remember: normally static data members are declared in the class declaration and initialized in the file containing the class method. Use the scope operator to indicate the class to which the static member belongs when initializing. However, if the static member is an integer or an enumerated const, you can initialize the!!! in the class declaration

Copy Code code as follows:

#include <iostream>
using namespace Std;
Class Test
{
Public
static int num;
};
int test::num = 0;
void Main ()
{
Cout<<test::num <<endl;
Test::num = 20;
Cout<<test::num <<endl;
}

Generally static data members are initialized outside of the class definition as a member function is defined outside the class definition, the name of the static member in such a definition must be qualified by its class name, as in the example above
int test::num = 0;
As with global objects, static data members can provide only one definition in the program, this means that the initialization of a static data member should not be placed in a header file but should be placed in a file containing a inline function definition of a class, and static data members can be declared as any type they can be const Object array or Class object, and so on
Copy Code code as follows:

#include <string>
Class Account {
// ...

Private
static const string name;
};
Const string Account::name ("savings account");


A const static data member that is ordered as a special case can be initialized with a constant value in the class body, for example, if you decide to store the name of an account with an array of characters instead of a string, then we can specify the length of the array with the Const data member of type int for example:
Copy Code code as follows:

Header file
Class Account {
// ...
Private
static const int namesize = 16;//like VC does not support this
static const char Name[namesize];
};
Text file
Cons Tint account::namesize;//required member definitions

const char account::name[namesize]= "savings account";


There's something interesting in this particular case. It is noteworthy that the const static data member of an ordered type initialized with a constant value is a constant expression constant expression, and the Class Designer can declare such static data members if it is necessary to use the named value in the class body. For example, because the const static data member Namesize is a constant expression, the designer of the class can use it to specify the length of the array data member name, and the member must still be defined outside the class definition when it initializes a const static data member in the class body
However, because the initial value of this static data member is specified in the class body, the definition outside the class definition cannot specify an initial value because the name is an array that is not an ordered type so it cannot be initialized in the class body, and any attempt to do so will result in compile-time errors such as:
Copy Code code as follows:

Class Account {
// ...
Private
static const int namesize = 16;//OK: ordered type
static const char name[namesize]= "savings account";//Error
};

The name must be initialized outside of the class definition, and this example also shows that the member namesize specifies the length of the array name, and the definition of array name appears outside the class definition.
const char Account::name[namesize] = "savings account";

The

Namesize is not qualified by the class name account. Although Namesize is a private member, the definition of name is still not wrong, how can this be? The definition of a class member function can refer to a static data member as well as a private member of the same data member. The definition of name is that it can refer to account's private data members within the domain of its class when the qualified decorated name Account::name is seen.

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.