C + + calls a member function of a null pointer object--static binding and dynamic binding

Source: Internet
Author: User

In recent code, I've seen the notation of a member function that calls a null pointer object , which reminds you of the last problem you encountered:

How member functions of a C + + class are stored (whether they belong to objects of the class)

The essence of the two is the same, the last time just briefly discussed, this time from the compiler's point of view, to talk about this knowledge point.

A simple example:

class MyClass  {  public:      int i;      void hello()      {          printf("hello\n");      }      void print()      {          printf("%d\n", i);      }  };  void main()  {      MyClass* pmy = NULL;      pmy->hello();   }  
The difference between "static binding" and "dynamic binding"

Take the following statement as an example:

somenull->foo();

The intent of the statement is to call the Foo member function of the object somenull.

In a dynamically bound language such as Java or Python, the compiler generates code that is presumably:
Locate the Foo member function of the object somenull, and call it. (Note that this is what is found when the program is running, which is what is called dynamic binding: The runtime binds the function name to its corresponding actual code.) Sometimes also called late binding. )

and for C + +, in order to ensure the efficiency of the program's running, C + + designers think that anything can be determined at compile time, do not drag to run the search again. So the C + + compiler sees this as a good thing to do:
1, find somenull type B, found that the type has a non-virtual member function called Foo.
2, found, here generates a function call, directly tune B::foo (Somenull).
So at runtime, because there is no code in the Foo () function that needs to dereference the Somenull pointer , the segment fault will not be raised in real-world situations. The parsing of member functions here, and the work of finding their corresponding code, are done in the compile phase rather than at runtime, which is called static binding , also called early binding.

So the real reason for the code to work correctly is that for non-virtual member functions, the C + + language is statically bound.

In C + +, each object has a pointer to its own, which is primarily used to distinguish between different objects, so that you can access the member variables of different objects based on it.

The first parameter of the compiler-compiled member function is the this pointer, which refers to the data member through the this pointer and calls other member functions.

    MyClass*=NULL;      pmy->hello();   

is actually equivalent to:

MyClass::hello(NULL);

The Hello function does not use any member variable in the class, so it will not use the this pointer, even if the this pointer is null at this point, and does not prevent us from using the Hello function, however, if you call Pmy->print (), then the null pointer error will be reported. Because this function attempts to access the member variable I with the this pointer, this is NULL.

It is important to note that virtual functions are generally dynamically bound ~

C + + calls a member function of a null pointer object--static binding and dynamic binding

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.