4 C + + base 4 class const function Go global function returns the *this array class. Friend function class operator overloading

Source: Internet
Author: User
Tags class operator




1, who is the const modification of a function in a class?

[Email protected]:~/c++$ cat main.cpp #include <iostream> #include <stdlib.h>using namespace Std;class a{ Public://const three ways of writing//const void fun (int a,int b)//void a const fun (int a,int b)//void fun (int a,int b) constvoid Fun (int a,i NT b) Const{a = 100;//const Modified Not a//this->a = 200;//compilation hint: ' A::a ' in read-only object}private:int a;int b;}; int main () {A A1;return 0;}


Answer:

Three ways to modify the const are the this pointer

const void Fun (int a,int b)

void const fun (int a,int b)

void Fun (int a,int b) const

The memory space pointed to by this pointer cannot be modified

Equivalent to void fun (const A *this,int a,int B)

Modifies the value of the this pointer itself, and the compilation does not pass




Convert a member function in a class to a global function

[email protected]:~/c++$ cat main.cpp  #include  <iostream> #include  < Stdlib.h>using namespace std;class a{public:void fun () {cout <<  "A=" < <this->a<< " b=" <<this->b<<  "  in fun" &LT;&LT;ENDL;} A (int a=0,int b=0) {this->a = a;this->b = b;cout <<  "A=" <<this->a<< " b=" <<this->b<< "  in init  \n";} A add (a &a) {a t (this->a + a.a,this->b + a.b); return t;} a  (const  a &obj) {cout <<  "in copy \n";} ~a () {cout <<  "a= " <<a <<  " b=" <<b << "  Free\n ";  } public:int  a;int  b;};/ /Turn member function into global function A fun (A&NBSP;&AMP;A1,A&NBSP;&AMP;A2) {a a3;return a3;} Void fun1 () {cout <<  "--------  in fun1 ------------------- \n"; (A&NBSP;A1); A&NBSP;A2 (3,4); A a3 = a1.add (A2); A3.fun ();} Void fun2 () {cout <<  "--------  in fun2 ------------------- \n"; (A&NBSP;A1); A&NBSP;A2 (3,4); A&NBSP;A3 (5,5);  a3 = a1.add (A2);         a3.fun ();} Int main () {fun1 (); fun2 (); return 0;} [email protected]:~/c++$ g++ -g main.cpp  && ./a.out --------   in fun1 ------------------- a=1 b=2  in init  a=3  b=4  in init  a=4 b=6  in init  a=4 b=6   in funa= 4 b=6 freea= 3 b=4 freea= 1 b=2  Free--------  in fun2 ------------------- a=1 b=2  in init   a=3 b=4&Nbsp; in init  a=5 b=5  in init  a=4 b=6  in  init  a= 4 b=6 freea=4 b=6  in funa= 4 b=6  Freea= 3 b=4 freea= 1 b=2 free



member function returns *this

[email protected]:~/c++$ cat main.cpp  #include  <iostream> #include  < Stdlib.h>using namespace std;class a{public:void fun () {cout <<  "A=" < <this->a<< " b=" <<this->b<<  "  in print" &LT;&LT;ENDL;} A (int a=0,int b=0) {this->a = a;this->b = b;cout <<  "A=" <<this->a<< " b=" <<this->b<< "  in init  \n";} Returns a reference that is equivalent to returning itself A& add (a &a) {this->a +=  a.a,this->b +=   a.b;return *this;} a  (const  a &obj) {cout <<  "in copy \n";} ~a () {cout <<  "a=" <<a <<  " b=" <<b << "   free\n "; } public:int  a;int  b;}; Int main () {a a1); A&NBSP;A2 (3,4); A1.add (A2); A1.fun (); return 0;} [email protected]:~/c++$ g++ -g main.cpp  && ./a.out a=1  b=2  in init  a=3 b=4  in init  a=4 b=6   in printa=3 b=4  freea=4 b=6  free[email protected]:~/c++$




The custom array class encapsulates a function that has the following

void Arr_set (int n,int value);

int arr_get (int n);

int Arr_len ();

File 1:

[Email protected]:~/c++$ cat my_arr.h #pragma onceclass arr{public:void arr_set (int n,int value); int arr_get (int n); int Arr_len (); ARR (int n); Arr (const arr &boj); ~arr ();p rivate:int len;int *arr;}; [Email protected]:~/c++$

File 2:

[email protected]:~/c++$ cat my_arr.cpp  #include   "My_arr.h" #include  <iostream >using namespace std;void arr::arr_set (int n,int value) {this->arr[n] =  value;} Int  arr::arr_get (int n) {return this->arr[n];} Int  arr::arr_len () {Return this->len;} Arr::arr (int n) {if (n<1) {len = 0;arr = null;} else{this->len = n;arr = new int [n];cout << n << "  init ...\n ";}} Arr::arr (const arr &obj) {this->len = obj.len;arr = new int [ This->len];for (int i = 0;i<this->len;i++) {this->arr[i] = obj.arr[i]  + 1;} cout << this->len<< " copy ...\n";} Arr::~arr () {if (arr != null) {cout << this->len<< " free ...\n"; Delete [] arr;len = 0;}} [email protected]:~/c++$

File 3:

[Email protected]:~/c++$ cat main.cpp #include <iostream> #include "my_arr.h" #include <stdlib.h>using namespace Std;int Main () {ARR a1); for (int i = 0;i<a1.arr_len (); i++) {a1.arr_set (i,i);} for (int i = 0;i<a1.arr_len (); i++) {cout << a1.arr_get (i) << "";} cout << Endl; ARR A2 = a1;//equals sign operation, the C + + compiler invokes the copy constructor for (int i = 0;i<a2.arr_len (); i++) {cout << a2.arr_get (i) << "";} cout << Endl;return 0;}

Compile run:

[email protected]:~/c++$ g++ -g main.cpp  my_arr.cpp && ./ A.OUT&NBSP;20&NBSP;INIT&NBSP, ..... 0    1    2    3    4     5    6    7    8     9    10    11    12     13    14    15    16     17&NBSP;&NBSP;&NBSP;&NBSP;18&NBSP;&NBSP;&NBSP;&NBSP;19&NBSP;&NBSP;&NBSP;&NBSP;20&NBSP;COPY&NBSP, ..... 1    2    3    4    5     6    7    8    9     10    11    12    13     14    15    16    17    18  &NBSP;&NBSP;&NBSP;19&NBSP;&NBSP;&NBSP;&NBSP;20&NBSP;&NBSP;&NBSP;&NBSP;20&NBSP;FREE&NBSP, ..... 20&NBSP;FREE&NBSP, ..... [email protected]:~/c++$







Why do you have a friend function?


Reduce system overhead and increase efficiency when data sharing between classes is implemented.

If a function in Class A accesses a member in Class B (for example, the implementation of a smart pointer Class), then the function in Class A is a friend function of Class B.

Specifically, in order to enable member functions of other classes to access the private variables of the class directly.

That is, allow the outside class or function to access the class's private and protection variables, so that two classes share the same function.


In fact, there are about two situations in which you need to use a friend function:

(1) A friend is required for some situations where operator overloading is used.

(2) Two classes to share data.

[Email protected]:~/c++$ cat main.cpp #include <iostream> #include <stdlib.h>using namespace Std;class a{ public:a (int a,int b) {this->a = A;this->b = b;} Private:int a;int b;}; void Fun (A *p) {//cannot access the private property int n =p->a in the outside of the class;} int main () {A A1 (); Fun (&A1); return 0;} Compile error: C[email protected]:~/c++$ g++-G main.cpp &&/a.out main.cpp:In function ' void fun (A *) ': Main.cpp:13:6: Err      Or: ' int a::a ' is private int A; ^


Adding this function as a friend function is OK.

Friend function has no relation to position

[Email protected]:~/c++$ cat main.cpp #include <iostream> #include <stdlib.h>using namespace Std;class a{ friend void Fun (A *p);p ublic:a (int a,int b) {this->a = A;this->b = b;} int get_a () {return this->a;} Private:int a;int b;}; void Fun (A *p) {//Cannot access private properties outside of class P->a = 10;int n =p->a;cout << n << Endl;} int main () {A A1 (&A1); cout << a1.get_a () << Endl;return 0;} [Email protected]:~/c++$ g++-G main.cpp &&/a.out 1010


Friend class:

[Email protected]:~/c++$ cat main.cpp #include <iostream> #include <stdlib.h>using namespace Std;class a{ Friend class b;private:int A;}; Class B{friend void Fun (A *p);p ublic:b (int A) {a1.a = a;//can directly modify the properties of the friend Class}int Get_a () {return this->a1.a;////can directly modify the properties of the friend class }private:int A; A A1;}; int main () {B B1 (one), cout << b1.get_a () << "\ n"; return 0;} [Email protected]:~/c++$ g++-G main.cpp &&/a.out 11[email protected]:~/c++$



operator Overloading , preliminary

Allow two classes to be added directly minus



Operator overloading is essentially a function

Contract operator Keywords

The C + + compiler will automatically look for operators


[Email protected]:~/c++$ cat main.cpp #include <iostream> #include <stdlib.h>using namespace Std;class a{ public:a (int a,int b) {this->a = A;this->b = b;} int printf () {cout <<a<< "" <<B<<ENDL;} int a;int b;}; A operator+ (a &a,a &b) {cout << "Hello world \ n"; A T (a.a+b.a,a.b+b.b); return t;} int main () {A A1 (); A A2 (2,2); A a3 = a1+a2;a3.printf (); return 0;} [Email protected]:~/c++$ g++-G main.cpp &&./a.out Hello World 3 3[email protected]:~/c++$


You can perform operator overloading in accordance with the

Can not perform operator overloading in accordance with

650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M02/83/89/wKiom1d1M76wiqDjAADL_VvWF3U603.png "style=" float: none; "title=" 1.png "alt=" Wkiom1d1m76wiqdjaadl_vvwf3u603.png "/>

650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M00/83/89/wKiom1d1M7_RQecpAAEb5c580R4388.png "style=" float: none; "title=" 2.png "alt=" Wkiom1d1m7_rqecpaaeb5c580r4388.png "/>





This article is from the "Soul Bucket" blog, please be sure to keep this source http://990487026.blog.51cto.com/10133282/1794745

4 C + + base 4 class const function Go global function returns the *this array class. Friend function class operator overloading

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.