const member function, inline function, friend function, static member

Original link: https://blog.csdn.net/qq_38646470/article/details/79795538
Column link: https://blog.csdn.net/column/details/20026.html
[TOC]
#1.const member function
const The modified member function
adds const after the member function, and const modifies the object pointed to by the this pointer, that is, to ensure that the object calling this const member function will not be changed within the function.

class Date
{
public :
void Display ()
{
    cout<<"year:" <<_year<< endl;
    cout<<"month:" <<_month<< endl;
    cout<<"day:" <<_day<< endl<<endl ;
}

void Display () const
{
    cout<<"year:" <<_year<< endl;
    cout<<"month:" <<_month<< endl;
    cout<<"day:" <<_day<< endl<<endl;
}
private :
    int _year ; // 年
    int _month ; // 月
    int _day ; // 日
};
void Test ()
{
    Date d1 ;
    d1.Display ();
    const Date d2;
    d2.Display ();
}

const objects cannot call non-const member functions but can call const member functions
non-const objects can call non-const member functions and const member functions
const member functions can call other const member functions but cannot call non-const member
functions A const member function can call other const member functions and non-const member functions

#2. Inline functions Functions
decorated with inline are called inline functions. When compiling, the C++ compiler will call the inline functions to expand, there is no overhead of function stacking, and inline functions improve the efficiency of program operation.

. inline is a way of exchanging space for time, eliminating the overhead of calling functions. So long code or functions with loop/recursion are not suitable for inlining.
. inline is just a suggestion for the compiler, the compiler will automatically optimize it. If the function defined as inline has loop recursion, etc., the compiler will ignore the inline when optimizing.
. Inline function definitions must be put together to become an inline function. Just putting inline before the declaration will not work.
. Member functions defined in a class are defined as inline functions by default.

However, the effect of the inline function cannot be seen under the general compiler, because the compiler will automatically optimize it. Next, let's check it under linux.

#3. Friend
Friend function:
In C++, a friend function allows access to any member of the class outside the class, just like a member function, a friend function is specified with the keyword friend.
. A friend function is not a member function of a class.
. Friend functions can access all members through the object, as well as private and protected members.
Friend function:

Tomomoto:

#4.static member

The optimization of constructing copy construction in #5.N
looks at a piece of code:

class AA
{};
AA f (AA a)
{
return a ;
}
void Test1 ()
{
AA a1 ;
a1 = f(a1);
}
void Test2 ()
{
AA a1 ;
AA a2 = f(a1);
}

void Test3 ()
{
AA a1 ;
AA a2 = f(f(a1));
} 

Answer the following questions: The copy constructor of AA
is called in Test1 . The copy constructor of secondary AA is called in Test2 . In Test3, the copy constructor of AA is called ___ times.


write picture description here

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324514038&siteId=291194637