In-class initialization of C + + static members

Source: Internet
Author: User

In general, the initialization of static members of C + + classes is not difficult to understand, but refers to the "in-class initialization" of C + + static members that is confusing.

Let's look at the following code:

1 //example.h2#include <iostream>3#include <vector>4 using namespacestd;5 6 classexample{7  Public:8     Static DoubleRate =6.5;9     Static Const intVecsize = -;Ten     Staticvector<Double>VEC (vecsize); One }; A  - //Example.cpp -#include"example.h" the - Doubleexample::rate; -vector<Double>Example::vec; -

We need to judge whether the declarations and definitions of the above static data members have errors and explain why.

First, keep in mind that, in general, you should not initialize a member inside a class, whether it is a static member or not.

Second, if you must initialize a static member within a class, you must meet the following criteria:

1) A static member must be a literal constant of type constexpr.

The so-called literal types are commonly encountered: arithmetic types, references, pointers, and so on. Literal constant types are const type arithmetic types, references, pointers, and so on.

The so-called constexpr is a constant expression, which means that the value does not change and the result is evaluated during compilation . For example, literals, or const objects initialized with constant expressions, are also constant expressions. To help the user check whether the value of the variable declared/defined is a constant expression, c++11 the new rule, allowing the variable to be declared as a constexpr type so that the compiler can verify that the variable is a constant expression.

2) The initial value provided to the static member must be a constant expression

Note: in C + + primer version fifth, it is said that only static members can be provided with a const integer type within the class initializer, and that the initial value of the const integer type must be a constant expression. I think it's wrong! See the following analysis for details.

With these two principles, we can verify the above code.

1)static Double rate = 6.5;

Obviously not satisfied with the first article: because rate is not a constant type. Change to constexprt static const double rate = 6.5

It can also be seen from here that the initial value must not necessarily be a const integer type.

PS: If we no longer add the Constexprt modifier here, the compiler will prompt the error: "Error: ' constexpr ' needed for in-class initialization of static data member ' const Double example::rate ' of non-integral type [-fpermissive]
The general meaning is that for the initial value of a non-const integer type, if it is a constant expression, we need to manually add the modifier constexprt in front.

As for the definition section in the Example. cpp file, since we have already initialized within the class, we do not need to define it outside of the class. If you do not want to define it, you must use the following format:

Example.cpp

constexpr const double Example::rate; The const can be deleted, because the CONSTEXPRT itself contains the const

2)static const int vecsize = ;

Vecsize is a const int, and is a constant expression--satisfies the first; provides an initial value of 20, is a constant expression--satisfies the second! And because it is a const int, the front can be constexpr without a modifier.

3)static vector<double> VEC (vecsize);

Error! A vector is a template that is not a literal constant type, so it does not satisfy the first article. Should be changed to static vector<double> VEC;//Only and can only be declared, cannot define

is defined:

static vector<< Span style= "color: #0000ff;" >double> Vec (< Span style= "color: #000000;" >example :: vecsize) ;

Now we can add the test code to the Example.cpp to test it:

1#include"example.h"2vector<Double>Example::vec (example::vecsize);3constexprConst Doubleexample::rate;4 5 intMain () {6 7Example::vec.push_back (10.5);8cout << example::vec.back () <<Endl;9cout << example::rate <<Endl;Tencout << example::vecsize <<Endl; One}

Execution Result:

[Email protected] virtual-machine:~/c++/7.5$./10.56.5

In-class initialization of C + + static members

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.