类的进阶知识 classes

来源:互联网 发布:淘宝如何参加活动 编辑:程序博客网 时间:2024/06/03 20:20

操作符重载

type operator sign (parameters) { /*...*/ }


能重载的操作符列表sign table


+    -    *    /    =    <    >    +=   -=   *=   /=   <<   >><<=  >>=  ==   !=   <=   >=   ++   --   %    &    ^    !    |~    &=   ^=   |=   &&   ||   %=   []   ()   ,    ->*  ->   new delete    new[]     delete[]

操作符的使用(语句调用)

ExpressionOperatorMember functionGlobal function@a+ - * & ! ~ ++ --A::operator@()operator@(A)a@++ --A::operator@(int)operator@(A,int)a@b+ - * / % ^ & | < > == != <= >= << >> && || ,A::operator@ (B)operator@(A,B)a@b= += -= *= /= %= ^= &= |= <<= >>= []A::operator@ (B)-a(b, c...)()A::operator() (B, C...)-a->x->A::operator->()-


// vectors: overloading operators example#include <iostream>using namespace std;class CVector {  public:    int x,y;    CVector () {};    CVector (int,int);    CVector operator + (CVector);};CVector::CVector (int a, int b) {  x = a;  y = b;}CVector CVector::operator+ (CVector param) {  CVector temp;  temp.x = x + param.x;  temp.y = y + param.y;  return (temp);}int main () {  CVector a (3,1);  CVector b (1,2);  CVector c;  c = a + b;  cout << c.x << "," << c.y;  return 0;}


关键字this

静态成员

Static members


// static members in classes#include <iostream>using namespace std;class CDummy {  public:    static int n;    CDummy () { n++; };    ~CDummy () { n--; };};int CDummy::n=0;int main () {  CDummy a;  CDummy b[5];  CDummy * c = new CDummy;  cout << a.n << endl;  delete c;  cout << CDummy::n << endl;  return 0;}





#include <iostream>
using namespace std;


struct A
{
int value;
A(int v){value = v;};

A& operator++()
{
value+=10;
return *this;
}

const A operator++(int)
{
A temp = *this;
value+=20;
return temp;
}
};


int main() {
A a(1);
a++;
cout << a.value << endl;
++a;
cout << a.value << endl;
return 0;
}

  • Success time: 0 memory: 3296 signal:0
    2131








附录:前自增和后自增的差别

http://blog.csdn.net/cuglij/article/details/2913282

前自增和后自增运算符++的重载

分类: 01.C++ basic 2510人阅读 评论(1) 收藏 举报
编译器语言class扩展c

很久以前(八十年代),没有办法区分++和--操作符的前缀与后缀调用。这个问题遭到程序员的报怨,于是C++语言得到了扩展,允许重载increment 和 decrement操作符的两种形式。 然而有一个句法上的问题,重载函数间的区别决定于它们的参数类型上的差异,但是不论是increment或decrement的前缀还是后缀都只有一个参数。为了解决这个语言问题,C++规定后缀形式有一个int类型参数,当函数被调用时,编译器传递一个0做为int参数的值给该函数。

increment的前缀形式表示“增加然后取回”,后缀形式表示“取回然后增加”。

  1. #include "stdafx.h"
  2. #include "assert.h"
  3. class A
  4. {
  5. public:
  6.     A(int i)
  7.         :m_i(i)
  8.     {
  9.     }
  10.     // ++i
  11.     A& operator++()
  12.     {
  13.         ++m_i;
  14.         return *this;
  15.     }
  16.     // i++
  17.     const A operator++(int)
  18.     {
  19.         A tmp = *this;
  20.         ++(*this);    // Implemented by prefix increment
  21.         return A(tmp);
  22.     }
  23.     int m_i;
  24. };
  25. int _tmain(int argc, _TCHAR* argv[])
  26. {
  27.     int i = 0;
  28.     int i1 = i++;   // i1 = 0; i = 1;
  29.     int i2 = ++i;   // i2 = 2; i = 1;
  30.     A a(0);
  31.     A a1 = a++;     // i1 = 0; i = 1;
  32.     A a2 = ++a;     // i2 = 2; i = 1;
  33.     
  34.     //a++++; //avoid
  35.     //++++a; //support
  36.     assert(i1 == a1.m_i);
  37.     assert(i2 == a2.m_i);
  38.     return 0;
  39. }

说明

1. 类中的++操作符号重载之后必须保证其语意与全局++相同。

2.为了区分前后,用++()表示前自增,用++(int)后自增。

3.因为按照前自增的标准定义,应该支持"++++a"的语法,而且两次前自增都应该是对a对象的自身操作,如果返回A类型,那第二次前自增调用的是临时对象的前自增操作。
4.后自增应该返回"const Complex".这可以防止形如"a++++"的用法。

5.一般通过前自增操作来实现后自增操作符函数。

 

参考
C++ articles:Guru of the Week #4 -- Class Mechantics

More Effective C++