C + + Learning Note 07

Source: Internet
Author: User

The definition of an operator function two: member functions

return type operator operator (other than the first operand of the parameter table)

Binocular: Operation result type operator operator (second operand)

Monocular: Operation result type operator operator ()

When defined as a member function, the first operand acts as the current object and does not need to be passed as an argument, only the remaining operands are passed. In an operator function, the first operand can be accessed by *this or this->.

    Rules can only be used with member functions of [], (), =,->, type conversions

If you have pointer members pointing to dynamic memory, you should also write your own copy operator functions to implement functions similar to copy constructors. Three big: Copy enough, assignment, destruction

//[]{}->=type These operators can only be member functions#include <iostream>#include<cstring>using namespacestd;classs{Char*p; intLen;Private: S (Consts& x);//Private copy construction, no copying Public: S (Const Char* str="") {len=strlen (str); P=New Char[len+1];    strcpy (P,STR); }    ~S () {Delete[] p;p=NULL; } S&operator=(Consts& S1) {//assign result member function with current object        if(&s1== This)return* This;//If the incoming parameter is self, return directlyLen =S1.len; Delete[] p;//release old dynamic memoryp=New Char[len+1];//open new dynamic memory for Pstrcpy (P,S1.P);//Copy the strings in dynamic memory .        return* This;//returns the current object} friend Ostream&operator<< (ostream& o,Consts&x) {        returno<<X.P; }    Char&operator[](intIndex) {//member functions        returnP[index]; }};intMain () {S, A, B ("Furong");//is initialization, not assignmentS C; //...C=a=b;//c.operator= (a.operator= (b))cout<<a<<","<<b<<","<<c<<Endl; b=b;//b.operator= (b)cout<<b<<Endl; cout<<a[3]<<endl;//operator[] a,3a[1]='o'; cout<<a<<Endl; System ("Pause"); return 0;}

    The type conversion operator function is formatted as

operator type name ()

The return type is not written, and the return type is consistent with the type name and can only be a member function.

#include <iostream>#include<string>using namespacestd;classperson{stringname; intAge ; floatsalary; Public: Person (Const CharNintAfloats): Name (n), age (a), salary (s) {}//In no particular order of Precedence    operator string(){returnname;} operator Double(){returnsalary;} operator int(){returnAge ;};intMain () {Person A ("Hibiscus", -,80000); stringinfo = A;//a.operator string ()    DoubleMoney = A;//A.operator double ()    intage = A;//a.operator Int ()cout<<info<<","<<money<<","<<age<<Endl; System ("Pause");}

When you use parentheses as an operator, the number of arguments is variable. function definition Format:

return type operator () (parameter table)

objects that support the parentheses operator are also called function objects, because the form used is particularly like calling a function.

#include <iostream>using namespacestd;classa{int*p; intLen; Public: A (intNintv=0):p (New int[n]), Len (n) { for(intI=0; i<len;i++) P[i] =v; }    void operator()(intStartintStep) {          for(intI=0; i<len;i++) P[i] = start + i*step; }    int operator()(){        intsum=0;  for(inti =0; i<len;i++) Sum + =P[i]; returnsum; } Friend Ostream&operator<< (ostream& o,Consta&x) {         for(inti =0; i<x.len;i++) o<<x.p[i]<<" "; returno; }};intMain () {A A (Ten); A (5,1);//Starting at 5, the step is 1 filled with this array a.operator () (5,1)cout<<a<<Endl; cout<<a () <<endl;//A.operator () ()cout<<a<<Endl; System ("Pause"); return 0;}

C + + Learning Note 07

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.