c/c++ 友元的简单应用
友元的简单应用
1,对象 + 对象,或者,对象 + 数字,可以用类的成员函数去重载+号函数,但是,数字 + 对象就不能用类的成员函数去重载+号函数了,
因为编译器会把数字 + 对象翻译成数字.operator+(const 类 &对象),因为数字不是类的对象,无法传递给类的成员函数this指针。
用友元去重载:数字 + 对象
2,用友元去重载:>>运算符和<<运算符。
其实用类的成员函数也可以重载<<运算符,但是使用起来比较怪异,不能使用cout << 对象,只能使用对象 << cout。
#include <iostream>
using namespace std;
class Imaginary{
friend Imaginary operator+(int i, const Imaginary &m);
friend ostream& operator<<(ostream &os, const Imaginary &m);
friend istream& operator>>(istream &is, Imaginary &m);
public:
Imaginary():real(0), imag(0){
cout << "c:" << this << endl;
}
Imaginary(int real, int imag):real(real), imag(imag){
cout << "c:" << this << endl;
}
Imaginary operator+ (const Imaginary &m){
return Imaginary (real + m.real, imag + m.imag);
}
Imaginary operator+ (int i){
return Imaginary(real + i, imag);
}
Imaginary& operator= (const Imaginary &m){
cout << "asign" << endl;
if(this != &m){
real = m.real;
imag = m.imag;
}
return *this;
}
ostream& operator<<(ostream& os){
os << "[" << real << "," << imag << "]";
return os;
}
~Imaginary(){
cout << this << endl;
}
private:
int real;
int imag;
};
Imaginary operator+(int i, const Imaginary &m){
return Imaginary(i + m.real, m.imag);
}
ostream& operator<<(ostream &os, const Imaginary &m){
os << "(" << m.real << "," << m.imag << ")";
return os;
}
istream& operator>>(istream &is, Imaginary &m){
is >> m.real >> m.imag;
return is;
}
int main(){
Imaginary m1(10, 20);
Imaginary m2(1, 2);
Imaginary m3 = m1 + m2;
Imaginary m4 = m1 + 10;
Imaginary m5 = 20 + m1;
cout << "a" << m5 << "aa" << endl;;
m5 << cout << "bb" << endl;
Imaginary m6;
cin >> m6;
cout << m6 << endl;
return 0;
}
c/c++ 友元的简单应用的更多相关文章
- C++Review3_关于C++各种概念的串联与梳理
经过前面两个Review,对代码复用,类的继承概念有了进一步理解. 这里再做一次复盘,把其他概念也串联起来构成一个知识框架. 首先是类和对象的概念.对象指的是特征与技能的结合体,面向对象编程思想的好处 ...
- C++ 引用计数技术及智能指针的简单实现
一直以来都对智能指针一知半解,看C++Primer中也讲的不够清晰明白(大概是我功力不够吧).最近花了点时间认真看了智能指针,特地来写这篇文章. 1.智能指针是什么 简单来说,智能指针是一个类,它对普 ...
- 自己实现简单的string类
1.前言 最近看了下<C++Primer>,觉得受益匪浅.不过纸上得来终觉浅,觉知此事须躬行.今天看了类类型,书中简单实现了String类,自己以前也学过C++,不过说来惭愧,以前都是用C ...
- C++学习笔记 构造&析构 友元 new&delete
构造&析构函数 构造函数 定义:与类同名,可以有参可以无参,主要功能用于在类的对象创建时定义初始化的状态,无返回值,也不能用void修饰,构造函数不能被直接调用,必须通过new运算符在创建对象 ...
- C++学习12 友元函数和友元类
友元函数和友元类在实际开发中较少使用,想快速学习C++的读者可以跳过本节. 一个类中可以有 public.protected.private 三种属性的成员,通过对象可以访问 public 成员,只有 ...
- C++学习笔记之友元
一.引言 C++控制对类对象私有部分(private)的访问,通常只能通过公有的(public)类方法去访问.但是有时候这种限制太严格,不适合特定的问题,于是C++提供了另外一种形式的访问权限:友元. ...
- c# 友元程序集
在团队开发中,如果一个程序集中要调用另外一个程序集,但是要被调用的那个程序集又不想用public来公开自己的类, 那么怎么办,就是用最后一种internal来用来做类的可见性了. 下面来看一个简单例子 ...
- Effective C++ 第二版 17)operator=检查自己 18)接口完整 19)成员和友元函数
条款17 在operator=中检查给自己赋值的情况 1 2 3 class X { ... }; X a; a = a; // a 赋值给自己 >赋值给自己make no sense, 但 ...
- C++之友元函数
1.为什么要引入友元函数:在实现类之间数据共享时,减少系统开销,提高效率 具体来说:为了使其他类的成员函数直接访问该类的私有变量 即:允许外面的类或函数去访问类的私有变量和保护变量,从而使两个类共享同 ...
随机推荐
- 使用docker部署flask遇到的问题
容器内能访问,但是外网映射了端口怎么也访问不了 解决方法: app.run() 添加参数host='0.0.0.0'
- OJ:奇怪的类复制
描述 程序填空,使其输出9 22 5 #include <iostream> using namespace std; class Sample { public: int v; // 在 ...
- idea中查看方法参数;查看类、方法、属性注释
Ctrl+P:查看方法参数Ctrl+Q:查看类.方法.属性注释
- 13 ,CSS 入门基础,行内排版内嵌式排版和外部排版样式
1.认识 CSS 2.传统 HTML 设计网页版面的缺点 3.CSS 的特点 4.CSS 的排版样式 13.1 认识CSS CSS的英文全名是 Cascading Style Sheets,中文可翻译 ...
- 封装个 Android 的高斯模糊组件
本篇文章已授权微信公众号 hongyangAndroid (鸿洋)独家发布 最近基于 Android StackBlur 开源库,根据自己碰到的需求场景,封装了个高斯模糊组件,顺便记录一下. 为什么要 ...
- 替换富文本里的px为rem
var content = '23px' content = content.replace(/(\d+)px/g, function(s, t) { s = s.replace('px', ''); ...
- react-conponent-secondesElapsed
<!DOCTYPE html> <html> <head> <script src="../../build/react.js">& ...
- js-dot.js
//小结// toExponential 保留小数点( 0-20 bit ) document.writeln(Math.PI.toExponential(0)); //3e+0 document.w ...
- angular 设置年份选择下拉框,并默认今年
<select ng-model="selectedYear" ng-change="yearChange(selectedYear)"> < ...
- MySQL5.7: Paging using Mysql Stored Proc
-- 查询外键 涂聚文 (Geovin Du) select concat(table_name, '.', column_name) as 'foreign key', concat(referen ...