C + + static member functions cannot be declared as const, volatile, virtual reason and C + + object model __jquery

Source: Internet
Author: User
Tags object model volatile

View C + + memory layout with Visual Studio command Prompt

http://blog.csdn.net/daydreamingboy/article/details/8982563
As for learning the memory layout, learn the tool, as if the person who learned arithmetic would use a calculator. In addition, gcc/g++ words,
The-fdump-class-hierarchy option is available.

From the "Deep Exploration of C + + object model Houtie translation" A little summary, experience.

Only one reason, the static member function has no this pointer and is not "hooked" to the instance (object) of the class.

So the const volatile virtual how these limitations function through the this pointer.

By example: 1 basis: What is this pointer. What's the use of it.

The this pointer points to an instance (object) of the class, called its current object.

Class A

{

int i;

void Foo () {

i++;

}

};

void A::foo (); is converted to an external, non-member, ordinary function: (The actual function name also contains the code of the class name and the formal parameter to differentiate between classes and overloads, but here for simplicity, Foo is still used)

void Foo (A * this) {//strict form should be a * const this

(this->i) + +;

}

Accordingly,

A Bar;

Bar.foo (); is converted by the compiler to: foo (&bar); No consideration of special handling of function names (see above), hereinafter


Extension, another example of a function with a return value :

Class A

{

int i;

int foo () {

i++;

return i;

}

}bar; will become:

void Foo (A * this, int & result) {

(this->i) + +;

result = this->i;

Return }

int a = Bar.foo (); will become foo (&bar, a);

If you call Bar.foo () directly, there will be temporary variables, regardless of code optimization.


"Return value is reference type" and "return value is pointer type" is the same. References, when necessary, are implemented by pointers (constant pointer type * const), so references must be initialized and represent that variable from start to finish, both in the compiler assembly (machine instruction) Implementation mechanism is the same. "Do not return a pointer (address value) for a local object, and do not return a reference to a local object." C + + is an object-oriented high-level language, however, author of this book Lippman that understanding the implementation mechanism of C + + is necessary for senior C + + programmers. If you don't know what the compiler does to your code, the efficiency of the code is always unknown. Embedded, hardware development of the powerful C programmers look at the C code, and even know it on the target platform of what the assembly is. This relationship between C language and assembly makes the C language suitable for some embedded, driving, operating system and other low-level development. While the designer of C + + wants to transparently process programmers at the language level (such as references), programmers still understand the bottom up in the face of C + +, which is designed to "provide OO with efficiency and compatibility C".


2 polymorphism and virtual functionsC + + polymorphism can not be separated from virtual functions and pointers (or references), indispensable. Class Base {virtual void foo () {}}; Class Derived1:public Base {void foo () {}}; Class Derived2:public Base {void foo () {}}; Base b;
Derived1 D1; Derived2 D2;

Base * ptr = b; Ptr->foo (); The base's ptr = D1 is invoked; Ptr->foo (); The call is Derived1 ptr = D2; Ptr->foo (); The call is Derived2.
B.foo ();  The D1.foo () of base is called;  The call is Derived1 's D2.foo (); The call is Derived2 but: b = d1; B.foo ()//is called base because B = D1; "Truncation" (sliced) b = d2; B.foo ()//is called base because B = D2; "Truncation" occurred (sliced)
If a class contains virtual functions, then each object of this class will have an extra pointer to the compiler (virtual table pointer vptr), a one-dimensional array of objects shared by this class (virtual function table), and the address of the virtual function is stored in the item of the one-dimensional array (slot Slot (in addition, the virtual function table also holds the type information, at the very beginning). "Regardless of the complex circumstances of multiple inheritance (or even virtual inheritance), many times it is a combination, not an inheritance. In this mechanism only, the difference between the parent class and the subclass object is that the virtual function table holds different addresses (type information is different). and the virtual function table, a class to prepare a good.  Ptr->foo () will be converted into: foo (PTR) by the compiler; * ((PTR->VPTR) [1]) (PTR); The index of Foo in the virtual function table is 1
This way, if the PTR is pointing to a parent class object, then Ptr->vptr points to the virtual function table of the parent class, thus invoking the Foo function of the parent class, and if PTR points to a subclass object, ptr->vptr the virtual function table that points to the subclass so that the Foo function of the subclass is invoked , thus realizing polymorphism. In the execution B = d1; , the Copy assignment operator (copy assignment operator) is invoked, and D1 vptr does not assign a value to B's vptr,b to maintain the original value unchanged, still pointing to the base's virtual function table.3 Const object and const member functionClass A {void Foo1 () {} void Foo2 () const{}}; const a A;  A.foo1 (); A.foo2 (); Ok
The const member function is actually using the const a * this pointer: void Foo2 () const{} is converted to void Foo2 (const A * this) {}//Strictly const A * const This ("Read Only" The subject of pointers and constant pointers here we go.
The const member function cannot modify the object so that the const object invokes only the const member function and cannot invoke a non-const member function.
4) Volatile object and volatile member functionThe truth is the same as the const, no longer repeated. Http://stackoverflow.com/questions/15283223/volatile-function
5) StaticClass A {static void Foo () {}] bar;
A::foo (); Calling a static member function does not require an object
Bar.foo (); Transformed into A::foo () by the compiler;
Static member functions do not have the this pointer, so only static members (belonging to classes) can be accessed, and non static members (belonging to objects) cannot be accessed directly.

 #include <iostream> #include <thread> using namespace std;
	Class foo{public:void Bar (int i) {cout << "ID:" << i << Endl;
};

};
	
	int main (int argc, char* argv[]) {Foo A; Thread T (&foo::bar, &a, 1);

	Compare the editor's handling of the member functions of a class t.join ();
return 0; }
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.