C + + class pointer initialization

Source: Internet
Author: User

The above code will print "A". When the C + + class pointer is not initialized, it is safe to invoke member functions inside the class without error. Check it out online:A class pointer that is initialized to NULL can safely invoke a class member function that does not involve a class member variable without error, but if a class member function is called with an error, then the naturally uninitialized class pointer also satisfies the situation, since it can be used if the assignment is null. Suppose you now have a simple class definition as follows:
Class Test
{
Public
void Func () {cout << "hahaha" << Endl;}
int get () {return a+b;}
Test (): A (1), B (2) {}
Public
int A, B;
};
The compiler will then automatically convert the class to:
Class Test
{
int A, B;
};
void _test_func (Test * this);
int _test_get (test* this);
........


The functions in the class are statically compiled by the compiler, and all non-virtual functions (virtual functions?). Don't worry, it will be explained later), because the function address compilation period has been determined. We know that a member function in a class invokes a member variable through the this pointer, and the compiler passes the this pointer as a default parameter to the class member function, such as myclass.function (int a,int b) and function (& Myclass,int A,int B)
Add the main function as follows:
int main ()
{
Test *p=null;
P->func ();//correct, no call member variable, no empty this pointer
P->get ();//error, the This pointer is empty, the variable is called through the this pointer, so an error occurs
return 0;
}
As a result, the Func () function that does not call the member variable executes correctly, calling the Get () function error of the member variable. Both actually pass in the empty this pointer, the former is only because there is no call to the this pointer, and the latter is called. (At this point P-func () and p->get () are equal to Func (null), get (NULL) ... The object pointer is null, and when the member function is called, the function address is determined during compilation, and the member function is not called by the object pointer (also the current P-pointer), and the object pointer passes in the function as a parameter and then calls the member variable.
OK now the problem is, if it is a virtual function, because the virtual function to calculate the vptr through the this pointer, and then find vtable, and then dispatch. Because this pointer is empty, it will be coredump when looking for vtable. In all cases, all functions that call the this pointer will have an error, and the member function that does not call the this pointer at all is fine.
Summary: At any time to define the pointer must be initialized, this is a good habit, the problem was originally due to write program negligence, but wrong to actually compile through, so at that time has not found this hand mistake, now traced to the internal mechanism of C + + has a deeper understanding, But also hint oneself can't have luck mentality, must develop good programming habit, whether for oneself or for future cooperation partner is a good thing.

C + + class pointer initialization

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.