The first knowledge of C + +, the difference between references

Source: Internet
Author: User

    Before explaining the problem, look at how an object in C can invoke the members of the object itself. I also recalled the question that Tom had asked Lucy to eat:

One. this pointer

  1#include <stdio.h>2   3 struct Person4 {  5     Char*name; 6     void(*FP)  (struct person* t); 7     Char*GF; 8   9 }; Ten   One voidHellostructperson*t);  A   - intMain () - {  the     structPerson Tom; -Tom.name ="Tom";  -TOM.GF ="Lucy";  -TOM.FP =Hello; +TOM.FP (&Tom);  -   +     structPerson Songchengyi; ASongchengyi.name ="Songchengyi";  atSONGCHENGYI.GF ="Pengpeng";  -songchengyi.fp=Hello; -Hello (&Songchengyi);  - }  -   -   in   -   to voidHellostructperson*t) + {  -printf"%s Please%s eat! \ n",t->name,t->gf);  the }  * 

    For the convenience of eating, we refer to a function pointer to Hello, in C + +, similar to the operation of Hello is called a method;

and struct struct person is called class;

In line 21 struct person Tom is called an instantiation of an object, and Tom is called an object;

To conveniently invoke the parameters within the class itself, we define a struct person* T, which can be easily invoked when the method is called.

 

In C + +, the This pointer of an object is not part of the object itself and does not affect the results of sizeof (the object). This scope is inside a class, and when a non-static member of a class is accessed in a non-static member function of a class, the compiler automatically passes the address of the object itself as an implied parameter to the function. That is, even if you do not write the this pointer, the compiler is compiled with this, it as an implicit parameter of non-static member functions, access to each member through this

    

  1#include <iostream>2   3 using namespacestd; 4   5 struct Person6 {  7     Char*name; 8     Char*GF; 9     voidhello ();Ten };  One   A intMain () - {  -     structPerson Tom; theTom.name ="Tom";  -TOM.GF ="Lucy";  -Tom.hello (); - }  +   -   + voidPerson::hello () A {  at   -cout<< This->name <<" please"<< This-&GT;GF <<"Eat"<<Endl; -   - }  -   -~

    

Two. References, references are much like pointers in C, but the references in C + + and the C-language pointers are significantly different:

1, the reference is to point to the same piece of memory, but the name is different, and the pointer is a variable to store the address;

2, the pointer can not be initialized, and the reference must be initialized;

。。。。 (also, add later)

    

  1#include <iostream>2   3 using namespacestd; 4   5 voidHelloint&j); 6   7 intMain ()8 {  9     intI=5; Ten     int*p = &i; One     int&j =i;//referring to the address of J for I;  A   -Hello (j); -   thecout <<"J is"<< J <<Endl; - }  -   -   + voidHelloint&j) - {  +j = -;  A   at }  -   -~

  References are specific to C + +.

   

    

The first knowledge of C + +, the difference between references

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.