C ++ basics -inline, const, mutable, this, static, volatile

C ++ basics -inline, const, mutable, this, static

https://www.cnblogs.com/hs-pingfan/p/10476447.html

The difference between mutable and volatile

https://blog.csdn.net/weixin_34284188/article/details/92920900

[C / C ++] C / C ++ which higher efficiency

https://blog.csdn.net/baishuo8/article/details/84428442

volatile:

volatile means "variable", but this explanation is simply a little misleading, should be interpreted as "direct access to raw memory address" more appropriate. "Volatile" is the presence of a relatively common and variable in terms of its value compiler (optimization) of unknown circumstances change (ie, not an assignment code execution by changing the value of the case), but due to external factors, such as multithreading and interrupt. When optimized, it sometimes takes some values ​​compilers time, direct access from the register, instead of getting it from memory, this optimization is no problem in single-threaded programs, but in a multithreaded program, due multiple threads are running concurrently, it is possible to put a thread of a common variable has changed, then the rest of the threads in the value of the register is outdated, but the thread itself does not know, that has not changed, still from the register acquisition, it causes the program to run in undefined behavior occurs. Not because the modification with volatile variable is "volatile", and if there is no external cause, namely the definition of the use of volatile, it will not change. The added volatile variables, the compiler will not perform its relevant code optimization, but to generate a corresponding code directly access the original memory address

 

First, the member function in the class definition inline

Member functions within the class that implements the functions defined in fact also called members of the class.

This function is defined in the class directly implemented, as will be inline with an inline process.

Second, the end of the const member functions

const: Constant, later adding a const member function. The need to increase not only in the const member function declaration, but also increase the const member function definition.

Role: tell the system, this member function does not modify the object inside the value of any variable, and so on, that is to say, this function does not modify any member state MyTime class.

Increase at the end of a function called constant const member functions.

Three, mutable

mutable, unstable and liable to change, the introduction of mutable const is to break through limitations

Once a member variable is mutable modified, and would indicate that the member variables can be modified forever in the state, even can also be modified at the end of the const member functions in.

const member functions, regardless of the object is const or non-const object can call const member functions

Rather than const member functions const object can not be invoked.

Fourth, returns a reference to the object itself, this

How to understand this? When calling the member function, the compiler will address responsible for this object (& mytime) passed to the member functions of a hidden this parameter.

In the system point of view, any access to the class members have been seen as doing this by implicitly called.

(1) this indicator can only be used in the member function, global function, you can not use this static function pointer

(2) In the ordinary function, this object is a pointer to a non-const const pointer (MyTime type, then this is MyTime * const this), this represents only refers to the current MyTime object.

(3) In the const member functions, this pointer is a pointer const const object (type MyTime, this is const MyTime * const this type of pointer)

MyTime mytime;
mytime.addMinute(10);
mytime.addSecond(0).addMinute(20);

Five, static members

 There is a member variable of the entire class, this member variable is static member variables (static member variables).

Features: does not belong to an object, belongs to the class, once we modify this value member variables in an object, you can see the results of the modified, among other objects.

This is only one copy of the member variable, a reference to that member variables, we use the class name :: member variable name.

Front can also add static member functions constitute a static member function is a member function of the entire class, when invoked with the class name :: member function name.

How to define static member variables: We generally at the beginning of a .cpp source file to define the static member variables. This would ensure that this function before calling any static member variable has been initialized.

Projects in this section:

Copy the code

#include <iostream>

#include "head.h"
using namespace std;

static int g_abc = 15; // stored in static memory

void func()
{
       static int abc = 5; // local static variables
       abc = 8; // The next time you perform static int abc = 5; this statement is not executed, the value of each abc is complete save the last modification of execution
}

// static member variables defined
int MyTime :: static_value = 15; // may not be initialized to 0 by default, does not require static definition of the time

int main ()
{
   MyTime mytime;
       mytime.addMinute(10);
       mytime.addSecond(0).addMinute(20);
       MyTime mytime1;
       mytime1.Minute = 15;
       MyTime mytime2;
       mytime2.Minute = 20;

       cout << mytime1.Minute << endl;
       cout << mytime2.Minute << endl;
       return 0;
}

// MyTime.h file
#ifndef __MYTIME__
#define __MYTIME__

class MyTime
{
public:
       // Member variables
       int Hour;
       int Minute;
       int Second;

       mutable int testValue;
    
 
private:
       // Member variables
       int Millsecond;
public:
       // member function
       void initMillTime(int tmpMillTime);
       // Constructor
       explicit Time(int tmphour,int tmpminute,int tmpsecond);
       Time(int tmphour,int tmpminute);
       explicit Time(int tmphour);
       explicit Time();
    static int static_value; // static member variable declarations
public:
       void addhour(int tmphour) const;
       //{
              //Hour += tmphour;
       //}

       MyTime& addMinute(int tmpMinute);
       MyTime& addSecond(int tmpSecond);
}

#endif

// MyTime.cpp file
#include "head.h"

void MyTime::initMillTime(int tmpMillTime)
{
       Millsecond = tmpMillTime;
}
       // Constructor
MyTime::Time(int tmphour,int tmpminute,int tmpsecond)
{
       Hour = tmphour;
       Minute = tmpminute;
       Second = tmpsecond;
}

MyTime::Time(int tmphour,int tmpminute)
{
       Hour = tmphour;
      Minute = tmpminute;
}

MyTime::Time(int tmphour)
{
       Hour = tmphour;
}

MyTime::Time()
{}

void MyTime::addhour(int tmphour) const
{
       // Minute = tmphour; // not allowed to modify any member of the class object inside
       testValue = tmphour; // before the variable to be changed to add a mutable can be modified
}

MyTime& MyTime::addMinute(int tmpMinute)
{
       Minute = tmpMinute;
       return * this; // returns the object itself
}
 MyTime& MyTime::addSecond(int Second)
 {
       this->Second = Second;
       return * this; // returns the object itself
}

Copy the code

Sixth, the class initialization

In C ++ 11 off, what I can offer an initial value within a class member variable, when we create an object, the initial value is used to initialize the member variables.

Seven, to initialize a const member variables in the constructor's initialization list be, can not be initialized by assignment.

Eight, the default constructor

Default constructor, that is no argument constructor is the default constructor.

No constructor, the class object how to call upon? This is called the default initialization, that class to perform default initialization process through a special constructor.

This particular constructor is called "default constructor." That is, no-argument constructor.

In the class definition, under the circumstances if no constructor, the compiler will automatically for us implicitly define a default constructor. Called "synthetic default constructor."

Once we wrote a constructor, regardless of the constructor with several parameters, the compiler will not create "synthetic default constructor" for us.

九、=default,=delete

= Default compiler can automatically generate a function of our body, generally only for the default special member functions

X. copy constructor

By default, one by one copy of each member variable of the class object when copy.

If the first argument to the constructor of a class is a reference class type belongs to, if there are other additional parameters, then these additional parameters also have default values, then the constructor called the copy constructor.

Function defaults should be on the function declaration, unless the function has no function declaration.

(1) The first argument is always copied with const

(2) explicit: Do not copy constructor declared as explicit general

Member function-by-function because there is a copy of the copy constructor of our own definition and lost a role.

Or that we own copy function to replace the default one by one copy of each variable behavior.

(A) If we do not define a copy constructor, the compiler defines one of us;

(B) If the compiler gives us a synthetic copy constructor, the copy constructor is also the member of the general parameters tmpTime (class objects) one by one to copy the object being created.

Type determines how each member a copy of it, say members of the variable is an integer, then the value directly copied;

If the member variable is a class type, then calls the copy constructor of the class to copy.

(C) if they define a copy constructor, then replaced the system synthetic copy constructor, this time, you have to give a class member variable assignment in their own copy constructor, to avoid class members have not been assigned to use It happens.

There are some situations will call the copy constructor circumstances occur:

(1) an object as an actual argument to a non-reference type of parameter.

(2) returns an object from a function of the time.

 

No pain no gain.

Released seven original articles · won praise 13 · views 20000 +

Guess you like

Origin blog.csdn.net/xingsongyu/article/details/103594192