C ++ object model Learning Record (4) function semantics (to be continued)

Source: Internet
Author: User

1. c ++ supports three member functions: static, non-static, and virtual functions.

2. Various Member call Methods

Non-static member functions. In C ++, all non-static member functions are converted into a global member function and implicitly passed to a Class Object Pointer, in addition, the efficiency of selecting member functions is the same as that of global functions, so there is no burden on them.

Virtual member function calls will be converted into a pointer pointing to vptr, and vptr pointing to virtual function table

For example

PTR-> normalize (), normalize () is a virtual member function that will internally convert to (* PTR-> vptr [1]) (PTR ),

Static member function. If normalize () is a static member function, then:

OBJ. normalize () and PTR-> normalize () will be converted to normal function bar, because static is stored outside the class object, normal_point3dsfv ()

The static modifier makes it impossible to use the object pointer:

(1) The non-static member in its class cannot be directly accessed. to access non-static data members within the scope of static, you must use the this pointer.

#include <iostream>

using namespace std;
class A
{
public:
A(int a)
{
this->a = a;
}
static int f()
{
//error: invalid use of member 'A::a' in static member function
//return a;
}
private:
int a;
};
int main()
{
A a(4);
int i = a.f();
return 0;
}

In fact, the use of member functions does not work either:

#include <iostream>

using namespace std;
class A
{
public:
A(int a)
{
this->a = a;
}
static int f()
{
//error: invalid use of member 'A::a' in static member function
//return a;

int i = this.g();
return i;
}
int g()
{
return a;
}
private:
int a;
};
int main()
{
A a(4);
int i = a.f();
return 0;
}

  

(2) cannot be declared as const, volatile, or virtual (this pointer is required)

(3) You can use class to directly call an object without calling the object.

Static member function, which can be a callback function

3. Virtual member functions: single inheritance, multi-inheritance, and virtual inheritance

 

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.