Dark horse programmer C + + learning record: class and object part3-4 object model and this pointer & friend

Posted by shiranwas on Thu, 23 Dec 2021 19:06:59 +0100

4.3 C + + object model and this pointer

1. Member variables and member functions are stored separately

In C + +, member functions and member objects are stored separately
1. Only non static member variables belong to the object of the class, and non static member variables occupy the object space;
2. Static member variables do not occupy the object space;
3. Functions do not occupy the object space, and all functions share a function instance;
4. Static member functions do not occupy object space;
Each non static member function will only produce a function instance, that is, multiple objects of the same type will share a piece of code;

The memory space occupied by empty objects is 1 (allocated to each object by the C + + compiler)
Empty objects should also have unique memory space. One byte is to distinguish the memory occupied by empty objects.
Memory alignment: the total size of the structure, which must be an integer multiple of the most basic internal member;

2.this pointer concept

1.this pointer points to the object to which the called member function belongs (class object, class is the abstraction of object, and object is the instance of class);
2.this pointer is a pointer implied in each non static member function;
this pointer does not need to be defined and can be used directly;

3. Purpose of this pointer:

  • When a formal parameter has the same name as a member variable, it can be distinguished by the this pointer
  • Return the object itself in the non static member function of the class. You can use return *this
  • If this pointer is used, it needs to be judged to ensure the robustness of the code

The difference between return reference and return value: return by reference will not create a new object, and return by value will produce a new object.

Why is there a this pointer?

1. Each non static member function will only generate one function case (multiple objects of the same type share one generation). The code distinguishes which object is called through this pointer.
2. After creating an object, you cannot use this pointer through the object. It is also impossible to know the position of each object pointer (this pointer is constructed before the execution of the member function, and it is clear after the execution. There is only this pointer in the member function)
3. In the member function, you can know the position of this pointer (& this)
4. Essence of this pointer: pointer constant (the value pointed to by the pointer cannot be changed, and the pointer can be changed)

3. Null pointer

Null pointer: a special pointer that does not point anywhere

Person * p = NULL;

Members can be accessed with null pointers, but to prevent them from crashing out of nothing,
This problem is more likely to occur when there is a this pointer;

if(this==NuLL)
(return ;)

4.const decorated member function

  • Add after member function: constant function
    (const here modifies the this pointer: the value pointed to by the pointer cannot be modified.)
    const person *const this person is the class name
    1. Member properties cannot be modified in a constant function
    2. Member attribute declaration with keyword mutable

  • Add before object: constant object
    Constant objects can only call constant functions
    (ordinary member functions can modify properties, but constant functions cannot modify member properties)

4.4 friends

  • In the program, some private properties also want to be accessed by special functions or classes outside the class, so you need to use friend technology; The purpose of a friend is to make a function or class
    Access private members in another class;
  • Friend keyword: friend
  • Three implementations of friends:
    ① Global function as friend
    ② Class as friend
    ③ Member function as friend

1 global function as friend

class Building
{
    //Tell the compiler that the goodGay global function is a good friend of the Building class and can access the private content in the class
    friend void goodGay(Building * building);

public:

    Building()
    {
        this->m_SittingRoom = "a living room";
        this->m_BedRoom = "bedroom";
    }


public:
    string m_SittingRoom; //a living room

private:
    string m_BedRoom; //bedroom
};


void goodGay(Building * building)
{
    cout << "Good friends are visiting: " << building->m_SittingRoom << endl;
    cout << "Good friends are visiting: " << building->m_BedRoom << endl;
}


void test01()
{
    Building b;
    goodGay(&b);
}

int main(){

    test01();

    system("pause");
    return 0;
}

2. Class as friend

class Building;
class goodGay
{
public:

    goodGay();
    void visit();

private:
    Building *building;
};


class Building
{
    //Tell the compiler that goodGay class is a good friend of Building class and can access the private content of Building class
    friend class goodGay;

public:
    Building();

public:
    string m_SittingRoom; //a living room
private:
    string m_BedRoom;//bedroom
};

Building::Building()
{
    this->m_SittingRoom = "a living room";
    this->m_BedRoom = "bedroom";
}

goodGay::goodGay()
{
    building = new Building;
}

void goodGay::visit()
{
    cout << "Good friends are visiting" << building->m_SittingRoom << endl;
    cout << "Good friends are visiting" << building->m_BedRoom << endl;
}

void test01()
{
    goodGay gg;
    gg.visit();

}

int main(){

    test01();

    system("pause");
    return 0;
}

3. Member function as friend

class Building;
class goodGay
{
public:

    goodGay();
    void visit(); //Only use the visit function as a good friend of Building, and you can send and access private content in Building
    void visit2(); 

private:
    Building *building;
};


class Building
{
    //Tell the compiler that the visit member function in the goodGay class is a good friend of Building and can access private content
    friend void goodGay::visit();

public:
    Building();

public:
    string m_SittingRoom; //a living room
private:
    string m_BedRoom;//bedroom
};

Building::Building()
{
    this->m_SittingRoom = "a living room";
    this->m_BedRoom = "bedroom";
}

goodGay::goodGay()
{
    building = new Building;
}

void goodGay::visit()
{
    cout << "Good friends are visiting" << building->m_SittingRoom << endl;
    cout << "Good friends are visiting" << building->m_BedRoom << endl;
}

void goodGay::visit2()
{
    cout << "Good friends are visiting" << building->m_SittingRoom << endl;
    //Cout < < good friends are visiting < < building - > m_ BedRoom << endl;
}

void test01()
{
    goodGay  gg;
    gg.visit();

}

int main(){

    test01();

    system("pause");
    return 0;
}

Topics: C++ data structure