cout流的重载
来源:互联网 发布:火狐javascript void 0 编辑:程序博客网 时间:2024/06/04 00:44
#include"aa.h"
#include<iostream.h>
#include<stdio.h>
#include<string.h>
class bb
{
public:
char *i;
bb ()
{this->i=0;
cout<<("no pram!\n");
}
bb (bb &b)//
{this->i=new char[10];
strcpy(this->i,b.i);
cout<<("深度复制!\n");
}
bb (char *i)//深度复制
{this->i=new char[10];
strcpy(this->i,i);
cout<<("with pram!\n");
}
~bb()//析构函数
{
if(0!=this->i)
delete this->i;
cout<<("destry!\n");
}
bb & operator=(bb &b)//对于要不要反悔值,是看如果在main中a=a1,就不要返回会也可以,如果是这样的a=a1=a2,就则就
定要反回值
{
this->i=new char[10];
strcpy(this->i,b.i);
return *this;
}
};
void operator<<(ostream &b1,bb &b2)
{
b1<<b2.i<<endl;
}
int main()
{
bb a("tom"),a1;
a1=a;
bb a2=a1;
cout<<a;
return 0;
}
在c++中怎么输出类中成员
cout<<a<<endl;则也会出错,为什么呢
下面是原因
#include"aa.h"
#include<iostream.h>#include<stdio.h>
#include<string.h>
class bb
{
public:
char *i;
bb ()
{this->i=0;
cout<<("no pram!\n");
}
bb (bb &b)//
{this->i=new char[10];
strcpy(this->i,b.i);
cout<<("深度复制!\n");
}
bb (char *i)//深度复制
{this->i=new char[10];
strcpy(this->i,i);
cout<<("with pram!\n");
}
~bb()//析构函数
{
if(0!=this->i)
delete this->i;
cout<<("destry!\n");
}
bb & operator=(bb &b)//对于要不要反悔值,是看如果在main中a=a1,就不要返回会也可以,如果是这样的a=a1=a2,就则就定要反回值
{
this->i=new char[10];
strcpy(this->i,b.i);
return *this;
}
};
ostream & operator<<(ostream &b1,bb &b2)//为什么有全局函数,是因为在cout的重载里只有对(1,2,4,8)有重载,所以我 们不能在类成员重载
{
b1<<b2.i<<endl;
return b1;
}
int main()
{
bb a("tom"),a1;
a1=a;
bb a2=a1;
cout<<a;
//其实是在这样远行的
::operator<<(cout,a).operator<<(endl);
return 0;
}
0 0
- cout流的重载
- (zz)对cout << 重载的源代码
- C++ 重载cout
- C++中Cout<<的运算符重载后优先级问题
- cout 流的重定向
- c++输出流cout的
- cout的输出流顺序
- cout流
- ostream类方法 重载的插入操作符 和 cout格式化
- ostream类方法 重载的插入操作符 和 cout格式化
- Cout流的格式控制整理
- C++输出流cout的顺序问题
- cout输出流的执行顺序
- cout输出流的执行顺序
- C++_重载运算符<< ostream流重载注意事项_使自定义数据可以用cout输出
- C++,重载输出操作符<<, cout.operator()
- cout<<endl;与cout<<"/n";的区别
- cout和std::cout的区别
- Android volley 解析(四)之缓存篇
- Android Studio导入GitHub上的项目常见问题(以图片轮播开源项目为实例)
- OpenGL Framebuffer Object (FBO)
- Mysql物理查询技术
- 双队列实现队列中元素排序
- cout流的重载
- AFNetworking NSURLErrorDomain error -1012
- Windows Phone 8.1添加后台任务运行条件(可添加多个)
- Qt元对象系统
- strcpy函数的实现
- 精通正则表达式(1)
- IOS:最简单一个使用自定义的TableViewCell例子
- 第1章2节《MonkeyRunner源码剖析》概述:边界(原创)
- HEVC的软解码和硬解码