#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。
对friend std::ostream & operator<< 的简单学习
最新推荐文章于 2022-04-28 16:08:14 发布