#include <iostream>using namespace std ;class Tree{ int height ;public: Tree(int treeHight):height(treeHight) {} ~Tree(){std::cout<< "*" ;} friend std::ostream & operator<< (std::ostream & os, const Tree * t) { return os << "Tree Height is: " <<t->height<<std:: endl; } }; void main(void){ Tree * t = new Tree(40) ; cout <<t <<endl; delete t ;}///:~
输出结果是:
Tree Height is: 40
*
说明:本例中对<<进行了重载,因为<<是在ostream中的,所以要加std::ostream & os。