[C ++ in-depth analysis learning summary] 24 classic problem analysis two

[C ++ in-depth analysis learning summary] 24 classic problem analysis two

Author CodeAllen , please indicate the source


1. Questions about destruction
When multiple objects exist in the program, how to determine the destruction order of these objects?

The order in which destructors are called when a single object is created

  • 1. Call the destructuring process of the parent class (explained in subsequent courses)
  • 2. Call the member variable destructor (the calling order is the same as the declaration order)
  • 3. Call the destructor of the class itself
    // the destructor is in the reverse order of the corresponding constructor.

Construction and destruction order

#include <stdio.h>

class Member
{
    const char* ms;
public:
    Member(const char* s)
    {
        printf("Member(const char* s): %s\n", s);
        
        ms = s;
    }
    ~Member()
    {
        printf("~Member(): %s\n", ms);
    }
};

class Test
{
    Member mA;
    Member mB;
public:
    Test() : mB("mB"), mA("mA")
    {
        printf("Test()\n");
    }
    ~Test()
    {
        printf("~Test()\n");
    }
};

Member gA("gA");


int main()
{
    Test t;
    
    return 0;
}

2. Answers about destructuring
For stack objects and global objects, similar to the order of stacking and stacking, the last constructed object is destructed first! !
The destruction of the heap object occurs when delete is used, and it is related to the order in which delete is used! !

3. Questions about const objects
Can the const keyword modify class objects? If so, what are the characteristics?

  • const keyword can modify objects
  • Const modified objects are read-only objects
  • Member variables of read-only objects are not allowed to be changed
  • The read-only object is a concept of the compilation stage, and is invalid at runtime

Const member function in C ++

  • const objects can only call const member functions
  • Only const member functions can be called in const member functions
  • const member function can not directly rewrite the value of member variables

Definition of const member function:

  • Type ClassName::function(Type p)const
  • After the member function declaration, before the function body
  • The function declaration in the class and the actual function definition must carry the const keyword

On the const function of class

#include <stdio.h>

class Test
{
    int mi;
public:
    int mj;
    Test(int i);
    Test(const Test& t);
    int getMi();
};

Test::Test(int i)
{
    mi = i;
}

Test::Test(const Test& t)
{
    
}
    
int Test::getMi()
{
    return mi;
}

int main()
{
    const Test t(1);
    
    t.mj = 1000;  //会报错,因为const修饰对象使其变为只读,不能出现在赋值符号左边
                  //
    return 0;
}

General projects do not define so many const functions, so you can directly call t.mi; (this is a private variable, but you can call it directly)

4.
Are member functions and member variables in question about class members belonging to specific objects?
From an object-oriented perspective

  • Objects are composed of attributes (member variables) and methods (member functions)
    from the perspective of program operation
  • Objects are composed of data and functions
    • Data can be located on the stack, heap and global data area
    • The function can only be located in the code segment

in conclusion

  • Each object has its own independent properties (member variables)
  • All objects share class methods (member functions)
  • Methods can directly access the properties of objects
  • The hidden parameter this in the method is used to refer to the current object

Member function secret

#include <stdio.h>

class Test
{
    int mi;
public:
    int mj;
    Test(int i);
    Test(const Test& t);
    int getMi();
    void print();
};


Test::Test(int i)
{
    mi = i;
}


Test::Test(const Test& t)
{
    mi = t.mi;
}
    
int Test::getMi()
{
    return mi;
}


void Test::print()
{
    printf("this = %p\n", this);
}


int main()
{
    Test t1(1);
    Test t2(2);
    Test t3(3);
    
    printf("t1.getMi() = %d\n", t1.getMi());
    printf("&t1 = %p\n", &t1);
    t1.print();
    
    printf("t2.getMi() = %d\n", t2.getMi());
    printf("&t2 = %p\n", &t2);
    t2.print();
    
    printf("t3.getMi() = %d\n", t3.getMi());
    printf("&t3 = %p\n", &t3);
    t3.print();
    
    return 0;
}

/*
t1.getMi() = 1
&t1 = 0x7fffe4bb2810
this = 0x7fffe4bb2810  
t2.getMi() = 2
&t2 = 0x7fffe4bb2818
this = 0x7fffe4bb2818
t3.getMi() = 3
&t3 = 0x7fffe4bb2820
this = 0x7fffe4bb2820
*/

Summary
destructor order opposite to the order of objects configured
const keyword can modify the object, the object to obtain a read-only
read-only objects can call member functions const
member functions of the class of all objects share
hidden this pointer used to indicate the current object

Published 315 original articles · praised 937 · 650,000 views

Guess you like

Origin blog.csdn.net/super828/article/details/100758855