C + + -- 03 scope and visibility of identifiers

Posted by Hieroglyphics on Wed, 05 Jan 2022 10:37:57 +0100

Tip: after the article is written, the directory can be generated automatically. Please refer to the help document on the right for how to generate it

#Foreword

1, Scope

1. Function prototype scope

The scope of function declarative formal parameter is the scope of function prototype in ().

2. Local scope

Within the definition of curly braces
{
}

3. Class scope

4. Namespace scope

In order to avoid naming crisis, two project teams use the same name when completing a project.
namespace{
}
Variables with namespace scope are also called global variables.

5. Special namespace

(1) Anonymous namespace, which can only be used in this file.

namespace
{
}

(2) Global namespace

	void bar()
	{
	}
	main
	{
		//Global namespace scope
		: : bar();
	}

2, Life cycle

Anonymous object: the lifetime is on one line.
All anonymous objects are r-values.
You can use frequent references to turn anonymous objects into an lvalue.
After const is added, the life cycle of anonymous objects to the end of program execution.

	int (1)
	const &ret = int (1);

3, Static member of class

Static data members:
Static members of a class have only one memory space, which is a property shared by class members.
Access method:
(1) If there is an object, it can be accessed through the object name.
(2) There is no object, which can be accessed in the form of class name:: + static data member. (traditionally, this range means that the attribute is static)
(3) Static data members. In the global area, static data members are not calculated during sizeof calculation.
Static member function:
Add static before the function name, which can be accessed in the form of class name:: + function name.
be careful:
Static member functions do not have THIS pointer.
Non static member functions cannot be accessed in static member functions of a class.
You can use static functions or static data members in non static functions.

4, Singleton mode

There are 23 design patterns.

1. Lazy mode

Dynamic loading, calling functions to create objects.

class Sigleton
{
  public:
    static Sigleton *creat()
    {
        if(m_p == NULL)
        {
            m_p = new Sigleton;
            count++;
        }
        return m_p;
    }
    static void destroy()
    {
        delete m_p;
        m_p = NULL;
        count--;
    }
    static int showCount()
    {
        return count;
    }
private:
    Sigleton(){}
    Sigleton(const Sigleton &){}
    static Sigleton *m_p;
    static int count;

};

2. Hungry Han mode

Static loading, creating objects before the main program runs.

class Sigleton
{
  public:
    static Sigleton *creat()
    {
        if(m_p == NULL)
        {
            m_p = new Sigleton;
            count++;
        }
        return m_p;
    }
    static void destroy()
    {
        delete m_p;
        m_p = NULL;
        count--;
    }
    static int showCount()
    {
        return count;
    }
private:
    Sigleton(){}
    Sigleton(const Sigleton &){}
    static Sigleton *m_p = new Sigleton  ;
    static int count;

5, Friend function of class

Break the access right outside the class, and the access right outside the class to private members and protected members in the class.
It can be designed as friend class or friend function.
Two classes cannot be friend classes to each other.

int Sigleton::count;

class Point{
public:

private:
    int m_x;
    int m_y;
    friend int Distance(Point p1,Point p2 );
};
int Distance(Point p1,Point p2 )
{
     return (p1.m_x-p2.m_x);
}
int main()
{
    Point p1,p2;
    Distance(p1,p2);
    cout<<"hello"<<endl;
    return 0;
}

6, Protection of shared data

Constant member function:
In C + +, adding const after a function indicates that the this pointer of the function is const modified. This function is called a constant member function.
The advantage is that it increases the range of parameter transmission.
Disadvantages: data members cannot be modified through the this pointer.
be careful:
Non member functions cannot be called within a constant member function.
Constant member functions can be called within non member functions.
const must be added at the definition and declaration.

7, volatile keyword

Prevent compiler optimization:
The compiler will save two copies of data in memory and registers. volatile is used to tell the compiler not to take data from registers but to read from memory.
In qt

		const int i(100);
		int*p = int(*)&i;
		*p = 1001;
		cout<<*p<<endl;
		cout<<i<<endl;		


Add volatile keyword:

		volatile const int i(100);
		int*p = const_cast<int *>(&i);
		*p = 1001;
		cout<<*p<<endl;
		cout<<i<<endl;		

8, Constant data member

Constant data members must be initialized and must be completed in the initialization list.

Topics: C++