Technical Answers Preliminary Understanding of Object-Oriented (C++ Class)

php是最好的语言
Release: 2018-07-23 15:58:40
Original
1507 people have browsed it

We should know that C has two kinds of member data: static, nostatic; three kinds of member functions: static, nostatic, virtual. In fact, for ordinary member functions, there is a this pointer in the parameter. This this pointer actually points to the object itself, through this pointer. The C compiler will know which object calls the member function of the class.

Are the member variables and member functions in C class objects stored together? if not?

We should know that C has two kinds of member data: static, nostatic; three kinds of member functions: static, nostatic, virtual.
Look at this case below:

class A
{    int a;    int b;
};class B
{    int a;    int b;    static int c;
};class C
{    int a;    int b;    static int c;public:    void func() {}    static void pg() {}
};int main()
{    //1. 分析A对象占的内存
    A a;    cout << sizeof(a) << endl;  //结果是8

    //2. 分析B对象占的内存
    B b;    cout << sizeof(b) << endl;  //结果是8

    //3. 分析C对象占的内存
    C c;    cout << sizeof(c) << endl;  //结果是8

    return 0;
}
Copy after login
Copy after login

In fact, based on the output results, we draw the following conclusions:
a. Member variables:

  • Ordinary members Variables are stored in objects and have the same memory layout and byte alignment as strutc variables;

  • Static member variables are stored in the global data area

b. Member functions are stored in code segments.


How does the C compiler manage classes and objects? Specifically, the object calls methods in the class. How does the C compiler distinguish which specific object calls which member method?

Technical Answers Preliminary Understanding of Object-Oriented (C++ Class)
In fact, for ordinary member functions (not static member functions), there is a this pointer in the parameter. This this pointer actually points to the object itself, through this pointer. The C compiler will know which object calls the member function of the class.


Let’s summarize:
a. Member variables and member functions in class C objects are stored separately;
b. Ordinary member functions of class C (including constructors, but (excluding static member functions) all contain a this pointer pointing to the current object;
c. Static member functions and static member variables belong to the class;
d. The difference between static member functions and ordinary member functions:
Static member functions do not contain a this pointer pointing to a specific object, while ordinary member functions contain a pointer pointing to a specific object.

Article Overview

We need to solve these problems:
a. Are member variables and member functions in class C objects stored together? if not?
b. How does the C compiler manage classes and objects? Specifically, the object calls methods in the class. How does the C compiler distinguish which specific object calls which member method?


Are member variables and member functions in class C objects stored together? if not?

We should know that C has two kinds of member data: static, nostatic; three kinds of member functions: static, nostatic, virtual.
Look at this case below:

class A
{    int a;    int b;
};class B
{    int a;    int b;    static int c;
};class C
{    int a;    int b;    static int c;public:    void func() {}    static void pg() {}
};int main()
{    //1. 分析A对象占的内存
    A a;    cout << sizeof(a) << endl;  //结果是8

    //2. 分析B对象占的内存
    B b;    cout << sizeof(b) << endl;  //结果是8

    //3. 分析C对象占的内存
    C c;    cout << sizeof(c) << endl;  //结果是8

    return 0;
}
Copy after login
Copy after login

In fact, based on the output results, we draw the following conclusions:
a. Member variables:

  • Ordinary members Variables are stored in objects and have the same memory layout and byte alignment as strutc variables;

  • Static member variables are stored in the global data area

b. Member functions are stored in code segments.


How does the C compiler manage classes and objects? Specifically, the object calls methods in the class. How does the C compiler distinguish which specific object calls which member method?

Technical Answers Preliminary Understanding of Object-Oriented (C++ Class)
In fact, for ordinary member functions (not static member functions), there is a this pointer in the parameter. This this pointer actually points to the object itself, through this pointer. The C compiler will know which object calls the member function of the class.


Let’s summarize:
a. Member variables and member functions in class C objects are stored separately;
b. Ordinary member functions of class C (including constructors, but (excluding static member functions) all contain a this pointer pointing to the current object;
c. Static member functions and static member variables belong to the class;
d. The difference between static member functions and ordinary member functions:
Static member functions do not contain a this pointer pointing to a specific object, while ordinary member functions contain a pointer pointing to a specific object.

Related recommendations:

Php object-oriented abstract class

PHP object-oriented programming detailed explanation: classes and objects_PHP tutorial

The above is the detailed content of Technical Answers Preliminary Understanding of Object-Oriented (C++ Class). For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template