24. Analysis of classic problems

When there are multiple objects in the program, how to determine the order of destruction of these objects?

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

1. Call Frey's construction process.

2. Call the constructor of the member variable (the calling order is the same as the declaration order).

3. Call the constructor of the class itself.

  Destructors are called in the reverse order of their corresponding constructors.

#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 ; // print order gA ->mA->mB->Test()->~Test->mB->mA->gA

    return 0;

}

For stack objects and global objects, similar to the order of stacking and popping, the last constructed object is destructed first.

Destruction of heap objects occurs when delete is used, which is related to the order in which delete is used.

2. Can the const keyword modify an object of a class? what special effects

The const keyword can modify the object. The object modified by const is a read-only object. The member variables of the read-only object are not allowed to be changed. The read-only object is a concept in the compilation phase and is invalid at runtime.

const member function in c++

    A const object can only call const member functions

    Only const member functions can be called from const member functions

    The value of the member variable cannot be directly overwritten in the const member function

const member function definition:

    Type ClassName::function(Type p) const

Both the function declaration in the class and the actual function definition must have the const keyword

#include <stdio.h>
class Test
{
    int mi;
public:
    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);     // 只读对象
    return 0;

}

3. Do member functions and member variables belong to specific objects?

From an object-oriented perspective, an object consists of properties (member variables) and methods (member functions)

From the perspective of program operation, objects are composed of data and functions. Data can be located in the stack, heap and global data area, and functions can only be located in the code segment. The code segment is read-only and cannot be changed when the program is running. When the compiler compiles the final executable program, the code segment is determined and cannot be changed. Snippets cannot be dynamically added and removed.

Each object has its own set of member variables, but all objects share a set of member functions.

Conclusion: Each object has its own independent properties (member variables), all objects share the method (member function) of the class, the method can directly access the properties of the object, and the hidden parameter this in the method is used to refer to the current object.

 the this pointer points to the address of the object that is currently calling the function

Teat::Test(const Test& t)

   Note: Is the following statement correct? Aren't objects inaccessible to private members?

   mi=t.mi; // There is only one set of member functions, and member functions can directly access the member variables of the corresponding class object

}

The member function can directly access the member variables of the object, and the copy constructor is a special member function that can directly access the member variables of the object.

#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;
}

The const keyword can modify an object to get a read-only object, and a read-only object can only call const member functions. All objects share the member functions of the class, and the hidden this pointer is used to represent the current object.

Guess you like

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