Introduction to C + + object Model (i)--"Deep Exploration of C + + object Model" thin notes

Source: Internet
Author: User
Tags function prototype
One

an object model with single inheritance and no virtual functions

Class A
{public
:
    int foo ()  {    return  val;    }
    static int Stafun ()  {     return  staval;  }
    static int staval;
Private:
    int val;
    char bit1;
} ;

Class B:public A
{public
:
    Char foo ()  {    return  bit2;    }
Private:
    char bit2;
};

Memory Layout:

static data members:

It is presented by the compiler outside of class and is treated as a global variable (but only visible within the class life span)

Each static data member has only one entity, stored in the data section of the program,

Through '. ' operator, which accesses a static data member, is only a grammatical Bingyi. Static data members are not actually in class object, so accessing it does not require class object.

Although you can access a static member without the class object, its access function is bound to a class object. (If the access control of a static member is protected or private, it must be accessed through the Access function)

"Note:" Static data members of the class must be defined globally before they can be used.

Regardless of its access control, the static data member must be defined before the main function.

For example: int a::staval = 0;

Otherwise, the compiler will make an error: undefined reference.

Because the compiler converts the use of static data members into direct use, static members are declared in class and cannot be seen externally. It needs to be defined externally to make the code visible later.

handling of member functions:

One of the design guidelines for C + +: Non-static member functions have the same storage efficiency as normal external functions.

The C + + compiler will translate member functions into general functions:

① rewrite the function prototype and insert an extra parameter this pointer. is used to provide an access channel so that the class object can invoke the function.

int A::foo (A * const this)

If the member function is a const, it becomes:

int A::foo (CONST * const this)

② access to non-static data members of class objects in the function body is accessed by the this pointer instead.

int A::foo (A * const this)

{return this->val; }

③ the member function back into an external function, processing the function name so that it becomes unique in the program.

After the conversion operation is over, each invocation operation is converted.

A Obja;
A * ptr = & obja;

Ptr->foo (); 
Obja.foo ();
were converted to:
Foo_inta (PTR);
Foo_inta (& Obja); 

static member function:

The main characteristic of a static member function is that it has no this pointer.

Therefore it:

① It is not able to directly access Non-static members in its class

② it cannot be directly declared as const, virtual

③ it does not need to be invoked through class object-though most of the time it is invoked like this.

Call to a static member function

Obja.stafun ();
Ptr->stafun ();
will be converted to:
stafun_staticintvoid ();
Stafun_staticintvoid ();

Static member functions are almost equivalent to external functions because of the lack of this pointer. It only acts on static data members of the class.

II

An object model with single inheritance and virtual functions

Cases:

Class A
{public
:
    virtual int foo ()  {    return  val;    }
    virtual int Funa () {}
private:
    int val;
    char bit1;
} ;

Class B:public A
{public
:
    virtual INT-foo ()  {return  bit2;    }
    virtual int Funb () {}
private:
    char bit2;

Memory Layout:

If a class has a virtual function, then each class object is placed on a pointer generated internally by the compiler, pointing to the table (virtual table).

The first item in virtual table is the type that represents the class.

Because of the specificity of the base class pointer, it can point to a base class object or to a derived class object. So: Ptr->foo (); This invocation, we need to know the true type of the object that PTR refers to. (Even if you don't know the type of the object that PTR refers to, you can call the fun function correctly, but because the fun function has a compiler-inserted this pointer, the this pointer corresponds correctly to the address of the object that the PTR points to, so that the member variables in the object are accessed correctly, but there is no

The table after virtual table is the address of each virtual function in class.

A class will only have a virtual table. The virtual table of the derived class is added on the virtual table of the base class, modified.

A virtual function in a derived class overwrites (overriding) a virtual function with the same name and parameters as the base class, overwriting the corresponding base class virtual function address in the Virtual table table to the address of the corresponding derived class virtual function.

All of the above work is done by the compiler. The execution period is to activate the corresponding virtual function in a particular virtual table entry, and then execute the virtual function correctly based on the type information of the first item in virtual table.

For example: Ptr->foo ();

Generally speaking, I do not know the true type of the object that PTR refers to. But I know. A virtual table of this object can be accessed via PTR.

Although I don't know which foo () entity will be invoked, I know that each of the Foo () function's addresses are placed in the second item of the virtual table.

Therefore: Based on the above information, the compiler can convert the call to:

(*ptr->vptr[1]) (PTR);

"Note:" The base class pointer can point to a derived class, but it actually points to the base class part in the derived class. (This does not violate the character of the pointer, the pointer type is consistent with its point of reference) so the base class pointer cannot access the members of the derived class. (But a base class pointer can indirectly manipulate a derived class member by accessing the virtual function of a derived class)


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.