C + + operator Knowledge Point 2

Source: Internet
Author: User

http://blog.csdn.net/szlanny/article/details/4295854

Operator it has two usages, one is operator overloading (operator overloading), and the other is operator casting (operation implicit conversion).

1.operator overloading
C + + can be overloaded with operator operators, in the following format: type T operator operator (), such as gravity Load +, as shown below

[CPP]View Plaincopy
    1. template<TypeName t> class A
    2. {
    3. Public
    4. Const T operator + (const t& RHS)
    5. {
    6. return this->m_ + RHS;
    7. }
    8. Private
    9. T m_;
    10. };

Another example is the function object in STL, overloaded (), which is the more recommended notation in C + +, similar to function pointers, as shown below

[C-sharp]View Plaincopy
    1. Template<typename t> struct A
    2. {
    3. T operator () (const t& LHS, const t& RHS) { return LHS-RHS;}
    4. };

2 operator Casting
C + + can be operator overloaded with implicit conversions in the following format: operator type T (), as shown below

[CPP]View Plaincopy
  1. Class A
  2. {
  3. Public
  4. Operator b* () { return this->b_;}
  5. operator Const b* () const {return this->b_;}
  6. Operator b& () { return *this->b_;}
  7. operator Const b& () const {return *this->b_;}
  8. Private
  9. b* b_;
  10. };

A;
When the if (a), compile, where it is converted to if (A.operator b* ()), in fact is to judge if (A.b_)

I have a close-up of 2 examples of these two usages.

Class Test//Overloaded classes
{
Public
Test (int a)
{
M_age = A;
}
~test () {}

Test operator = (test &temp)
{
This->m_age = Temp.m_age;

return *this;
}

Test operator + = (Test & TEMP2)
{
This->m_age + = Temp2.m_age;

return *this;
}

Public
int m_age;

};


Class TESTB//Implicit conversion
{
Public
Testb () {}
~testb () {}

char* Getstr ()
{
memset (m_buf, 0, 30);
strcpy (M_BUF, "Get Str ()");
printf ("Get str () \ n");

return m_buf;
}

Operator char* ()
{
memset (m_buf, 0, 30);
strcpy (M_BUF, "char *");
printf ("Char *\n");

return m_buf;
}

Public
Char m_buf[30];

};


int main ()
{
Test A (5), B (15);

A = b;
printf ("a.m_age=%d\n", a.m_age);//15

A + = B;
printf ("a.m_age=%d\n", a.m_age);//30

TESTB BB;
char *ptest1 = BB; printf char * Here TESTB is converted to char *
Char *ptest2 = BB.GETSTR (); printf Get str ()

return 0;

}

C + + operator Knowledge Point 2

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.