Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How do static members in C++ declare

2025-03-25 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)05/31 Report--

This article mainly explains "how to declare static members in C++". Interested friends may wish to have a look at it. The method introduced in this paper is simple, fast and practical. Next, let the editor take you to learn "how to declare static members in C++".

Introduction

Sometimes you need some members of the class to be associated with the class itself, rather than with each object of the class. For example, when all objects of a class share variables, we need to use static members of the class.

Declare static members of a class

The way to declare static members is to use the static keyword.

Static members can be public or private.

For example, define a class that represents the bank's account record:

Class Account {public: / / other non-static functions and data members / / static functions static double get_rate () {return interestRate;} static void set_rate (double r) {interestRate = r;} private: all objects of the static double interestRate;// class share the same interest rate / / other static private functions / / other non-static functions and data members}

Note:

The static member of the class exists outside any object, and the object does not contain any data related to the static data member.

Static member functions are not bound to any object and this pointers cannot be used in static member functions. Static member functions cannot be declared as const.

Use static members of a class

Use the scope operator:: to access static members directly.

Double r _ t _ r = Account::get_rate ()

Although static members do not belong to any object of the class, static members can still be accessed through the object of the class.

Account ac1;Account * ac2 = & ac1;double r = ac1.get_rate (); r = ac2- > get_rate ()

Member functions can use static members directly without the need for scope operators.

Define static members

Define static member functions

Static member functions of a class can be defined either outside the class or inside the class (note the difference between definition and declaration).

The static keyword cannot be used when a static member function is defined outside the class, and the static keyword is only used at the declaration of the static member function within the class. Or repeat.

When you define a static member function outside a class, you must indicate the class to which the function belongs, such as:

Class Account {public: / / other non-static functions and data members / / static member functions static double get_rate () {return interestRate;} static void set_rate (double r) {interestRate = r;} static void print (); / / static member functions declare private: static double interestRate / / all objects of this class share the same interest rate / / other static private functions / / other non-static functions and data members}; / / you do not need to use the static keyword when defining static functions, otherwise repeat. It also indicates the class to which the function belongs. Void Account::print () {/ / work to be done}

Define static data members

Because static data members do not belong to any of the objects of the class, they are not defined when the object of the class is created. Static data members cannot be initialized inside the class, and each static data member must be defined and initialized outside the class.

Double Account::interestRate = initRate (); / / do not use the static keyword

In-class initialization of static data members

As mentioned earlier, static data members of a class should not be initialized inside the class. However, if the static data member is also a constexpr type, it can be initialized within the class.

Even if a constant static member is initialized within the class, it should usually be defined outside the class, but the out-of-class definition makes it impossible to specify the initial value because the initial value is already provided within the class.

Special application scenarios for static data members

1. A static member is independent of any object, so the type of a static data member can be the class type to which he belongs, and a non-static data member can only be declared as a pointer or reference to the class to which he belongs.

For example:

Class Person {public: / /... private: static Person p; / / correct, static data members can be incomplete type Person * p1; / / correct: pointer members and references can be incomplete types Person & p2 / / correct Person p3; / / error: data members must be fully typed. }; / / before that, after class Person, Person classes are all incomplete types, because they are only declared but not yet defined

two。 You can use static members as default arguments

Class Screen {public: Screen& clear (char = bkground); private: static const char bkground;}

Non-static data members cannot be used as default arguments, because non-static data members belong to objects, and the values of objects are determined at run time, but the default parameters are determined at compile time, that is, no real object has been created when the default parameters are determined, so non-static data members cannot be used as default parameters, otherwise an error will be raised.

At this point, I believe that everyone on the "C++ static members how to declare" have a deeper understanding, might as well to practical operation it! Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report