C++重载流操作符的例子 不指定

Overload operator <<

felix写的一个简单例子:
#include <iostream>
using namespace std;
class testclass{
public:
  int a, b;
  testclass(int a1, int b1){
    a = a1, b = b1;
  }
  
  friend ostream & operator <<(ostream & os, testclass a1){
    cout << "a = " << a1.a << ", b = " << a1.b << endl;
    return os;
  }
  
  friend ostream & operator <<(ostream & os, testclass *a1){
    cout << "a = " << a1->a << ", b = " << a1->b << endl;
    return os;
  }
};//end of testclass

int main(){
  testclass *a1 = new testclass(1, 2);
  cout << a1;
  cout << *a1;
  getchar();
  return 0;
}

下面是更详细的重载各个流操作符的例子,原出处是Thinking In C++
copy 自 http://www.cndev.org/forum/msg.aspx?pid=233806

C++ 中重载 + 操作符的正确方法 不指定

from http://www.vckbase.com/document/viewdoc/?id=1356

C++ 中重载 + 操作符的正确方法

作者:Danny Kalev
编译:MTT 工作室

原文出处:Overloading Operator + the Right Way

摘要:本文概要性地介绍如何选择正确的策略来为用户定义类型重载 + 操作符。

收藏一段很有意思的代码 不指定

详见 http://hi.baidu.com/neuron13/blog/item/6910ff1e63a2d3f01ad576f5.html
#include<stdio.h>

int main(){
    int i,j;
    for(i=0;i<32;i++){
        for(j=0;j<32;j++){
            if((i+j)==(i^j)){
                printf("o");
            }else{
                printf(" ");
            }
        }printf("\n");
    }
    return 0;
}