Deep Exploration of C + + object model--function semantics

Source: Internet
Author: User

How member functions are called nonstatic Member function (non-static member functions)

-nonstatic Member Function Design guidelines: The same execution efficiency as the general non-member functions (nonmember). thus, in fact, the compiler internally translates the nonstatic Member function entity into a peer nonmember function entity.
-Conversion Process:
1. Add additional this pointer to provide a data access pipeline
2. Modify the function Body member data access method, use the this pointer to access
3. Re-"mangling" the function name modifier processing

Class a{ Public:intx, y;intFunc ();}intA::func () {returnX+y; }//nonstatic Member functionintA::func (A *Const  This)//1. Add extra parameter this pointerintA::func (A *Const  This){return  This->x + This->y;}//2. Accessing member data by the this pointer insteadextern intFUNC_1AFV (A *Const  This)//3. "mangling" modifier for function nameObj.func ()---> FUNC_1AFV (&obj)
Name Special handling (name mangling):

member function is overloaded to provide a unique name, using this technique.
Letternumbernamecalled+classnamecalled+ReferencenumbernumberMesh+Referencenumberclasstype

//可能的修饰方法:int Point::foo(float x)   int foo_5PointFf(float x)int Point::foo(void)   int foo_5PointFv()
Static Member function (statically member functions)
-静态成员函数没有this指针: 1. 不用直接存取class中nonstatic members 2. 不能被声明为const, volatile 或者virtual 3. 不需要有class object才能被调用-Static Member Function会被编译器转化为nonmember形式,实际上该类函数产不多等同于nonmember function。
int A::func()   //静态成员函数//各种方式调用静态函数,均转换成通过类作用于直接调用ptr->func()/obj.func()/A::func() --> A::func()//函数mangling修饰A::func() ---> func_1ASFv()  //SFv表示静态成员函数,参数void
Virtual Member function (virtual member functions)
//1是virtual table slot索引,关联到func函数ptr->func() -->(*ptr->vptr[1])(ptr)//明确的调用可以抑制虚拟机制A::func()// 通过该obj调用不支持多态obj.func() ---> func_1AFv(&obj)

-Calling virtual function via class object will be converted by the compiler to the general nonmember member function, which does not render polymorphic.

The following is a call to a virtual function.

Virtual Member Functions

C + + polymorphism: A derived class object is addressed with a public base class pointer.

//调用虚函数,如何在执行期确定func()实体ptr->func()
    • Things to do during compilation:
      -Vptr for each class object generated internally by the compiler
      -Give VTBL a fixed slot for each virtual function that remains associated with a specific virtual Funciton throughout the inheritance system

    • The virtual function included with VTBL is:
      -The function entity defined by the class, including the virtual function in the base class
      -Inherit from the base class entity and decide not to overwrite
      -pure_virtual_called () pure virtual function

Single inheritance
Class Point { Public:Virtual~ Point();//slot 1    Virtualpoint& mult (float) =0   //slot 2    floatX ()Const{return_x;}Virtual floatY ()Const{return 0;}//slot 3    Virtual floatZ ()Const{return 0;}//slot 4protected: Point(floatx =0.0);float_x; }class point2d: PublicPoint { Public:    ~point2d();//slot 1point2d& mult (float)//function overlay Slot 2    floatY ()Const{return_y;}//function overlay Slot 3protected:float_y; }class Point3D: PublicPOINT2D { Public:    ~Point3D();//slot 1point3d& mult (float)//function overlay Slot 2    floatZ ()Const{return_z;}//function overlay Slot 4protected:float_z; }//In general we do not know the actual type of the object PTR refers to in advance//So I don't know which mult entity will be called, but mult () fixed in slot 2 is OKPtr->mult ()//So the compiler translates as follows//Execution period the only thing to do is to determine which slot 2 entity(*ptr->vptr[2]) (PTR)

Multiple inheritance
//编译时期就将指针转移至第二个base class起始地址new Derived;//执行时需要调整pbase2至Derived起始地址delete pbase2;   //该行代码无法在编译时知道pbase2的真实对象,执行时才知道

-Multiple inheritance structures are as follows: Derived will have two VTBL generated by the compiler: (1) The principal entity, with the BASE1 (leftmost base class) common (2) secondary entity, related to BASE2

-Three ways to adjust the pointer
1 Call derived class virtual function by pointing to the BASE2 pointer.

//编译时期就将指针转移至第二个base class起始地址new Derived;//执行时需要调整pbase2至Derived起始地址delete pbase2;

2 Invokes an inherited virtual function in the second base claass by a pointer to the derived class.

//编译时期就将指针转移至第二个base class起始地址*=new Derived;//执行时需要调整pder以指向Base2 subobjectpder->mumble;   //调用Base2::mumble()

33 Clone function Entity return value types are different

//编译时期就将指针转移至第二个base class起始地址Base2  new Derived;//调用Derived* Derived::clone()//需要将pbase2指针调整至Derived class起始处//返回值时又需要重新调整至Base2 subobjectBase1 *pbase2_new = pbase2->clone;
Virtual inheritance

See Links:
http://blog.csdn.net/isunn/article/details/45535951

Point to member function pointer point to nonvirtual function pointer

-Get the real address, but it needs to be bound to a class object before it can be called through.

double (Point::*coord) = &Point::x;  //nonvirtual function//调用函数(obj.*coord) ()//被编译器转化(coord)(&obj)  //&obj为this指针

The-static member function address is a normal function pointer because there is no this pointer.

point to virtual function pointer

The virtual function address is unknown at compile time, but the index value of the VTBL slot associated with it is fixed. So the virtual function address can only get its index value.

class Point{public:    virtual ~Point();   //slot 1    virtualfloat z();  //slot 2}float (Point::*pmf) = Point::z;   //= 2new Point2d;ptr->*pmf;//被编译器转化为(*ptr->vptr[(int)pmf](ptr));
member function pointer under multiple inheritance

Question: How do I distinguish between a member function pointer pointing to an address or a virtual table index?
Methods: Modify member function pointer mptr structure, mptr->index positive and negative discriminant.

struct __mptr {    int delta;    int index;  //virtual function时:vtbl索引 nonvirtual时为-1    union{        ptrtofunc faddr;   //nonvirtual 时函数地址        int       v_offse;    }}

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Deep Exploration of C + + object model--function semantics

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.