c friend -- 友元
c friend -- 友元
友元用于突破protected 或者 private 保护的限制,首先要做的是在被访问者的类中声明是友元函数或者友元类。代码如下
- #include <iostream>
- using namespace std;
- class Square{
- private:
- int side;
- public:
- Square(int a):side(a){}
- friend class Rectangle ; //declare the Class is friend
- };
- class Rectangle {
- private:
- int width, height;
- public:
- Rectangle(int a, int b):width(a),height(b){}
- void set_values (int a, int b){
- width = a;
- height = b;
- }
- int girth();
- friend int area (Rectangle &); //declare the friend function here
- int get_width(){return width;}
- int get_height(){return height;}
- void conver_from_square(Square &s){
- width = height = s.side; //access so easily
- }
- };
- //implement the function here , access easily ,too
- int area (Rectangle &r){ return ( r.width * r.height); }
- int r_area(Rectangle &r){ return r.get_width() * r.get_height();}
- int Rectangle::girth(){return width + width + height + height; }
- int main () {
- test_sizeof:
- cout << "sizeof: Square " << sizeof(Square)
- << ",\tRectangle " << sizeof(Rectangle) << "\n\n" ;
- test_access:
- Rectangle r(2,3);
- cout << "area:" << area(r) << "\tgirth:" << r.girth() << endl;
- cout << "onather way:area " << r_area(r) << endl;
- Rectangle r1(2,3);
- Square s(5);
- r1.conver_from_square(s);
- cout << "rectangle convering from square , girth is " << r1.girth() << endl;
- return 0;
- }
结果
- sizeof: Square 4, Rectangle 8
- area:6 girth:10
- onather way:area 6
- rectangle convering from square , girth is 20
看函数
- area
- r_area
- girth
如果不是友元函数或类,访问情况如 r_area()函数,友元函数就可以直接访问成员。但是和成员函数比起来还是要有区别的,看函数girth()
看看size,友元类或者友元函数并不增加类的大小,只是声明一下。
c friend -- 友元的更多相关文章
- C++的友元类和友元函数实例
#include <math.h> #include<iostream> using namespace std; class Point { public: Point(do ...
- C++学习笔记 构造&析构 友元 new&delete
构造&析构函数 构造函数 定义:与类同名,可以有参可以无参,主要功能用于在类的对象创建时定义初始化的状态,无返回值,也不能用void修饰,构造函数不能被直接调用,必须通过new运算符在创建对象 ...
- c++友元函数
c++友元函数分两类: 一://友员全居函数 /*#include <iostream>using namespace std;class aaa{ friend void prin ...
- 重载运算符:类成员函数or友元函数
类成员函数: bool operator ==(const point &a)const { return x==a.x; } 友元函数: friend bool operator ==(co ...
- C++之友元
友元提供了不同类的成员函数之间.类的成员函数与一般函数之间进行数据共享的机制.通过友元,一个不同函数或另一个类中的成员函数可以访问类中的私有成员和保护成员.C++中的友元为封装隐藏这堵不透明的墙开了一 ...
- 不可或缺 Windows Native (20) - C++: 友元函数, 友元类
[源码下载] 不可或缺 Windows Native (20) - C++: 友元函数, 友元类 作者:webabcd 介绍不可或缺 Windows Native 之 C++ 友元函数 友元类 示例演 ...
- InternalsVisibleToAttribute——把internal成员暴露给指定的友元程序集
友元程序集简介 我们知道一个类中被定义为internal的成员(包括类型.方法.属性.变量.事件)是只能在同一个程序集中被访问到的(当然了,我这里说的是正常的方式,不包括通过反射来访问).这个规则在. ...
- c++ 操作符重载和友元
操作符重载(operator overloading)是C++中的一种多态,C++允许用户自定义函数名称相同但参数列表不同的函数,这被称为函数重载或函数多态.操作符重载函数的格式一般为: operat ...
- [Reprint]C++友元函数与拷贝构造函数详解
这篇文章主要介绍了C++友元函数与拷贝构造函数,需要的朋友可以参考下 一.友元函数 1.友元函数概述: (1)友元函数是定义在一个类外的普通函数.友元函数和普通函数的定义一样;在类内必须将该普通函 ...
- C++——友元、异常和其他
一.友元 类并非只能拥有友元函数,也可以将类作为友元.在这种情况下,友元类的所有方法都可以访问原始类的私有成员和保护成员.另外,也可以做更严格的限制,只将特定的成员函数指定为另一个类的友元.哪些函数. ...
随机推荐
- Windows多线程
//简单的引出多线程是肿么回事儿....当点击下载的时候,下载内容还没结束也可以点击资源库,其实这就用了另一个线程,弹出“下载完成”对话框的时候,没有点击确定是不能点击主页面内容的,这就是用----- ...
- 编绎OpenJDK
因为对于Java里的vtable,itable,有个地方还没搞明白,不得已去下个OpenJDK来研究下. 本来很不愿意去编绎OpenJDK,因为很有可能做的只是无用功,还有可能要去解决各种找不到链接库 ...
- MongoDB导出-导入-迁移
linux环境下,将mongodb迁移到同机器,不同端口上. 命令参数: [mongodb@pera bin]$ ./mongodump --help Export MongoDB data to B ...
- OGR 官方文档
OGR 官方文档 http://www.gdal.org/ogr/index.html The OGR Simple Features Library is a C++ open source lib ...
- Swift 与 Objective-C混合编程
在Swift项目中想要同一时候加入Objective-C的库支持或者须要同一时候用Objective-C编程 在加入新的文件时选择Objective-C系统就会自己主动生成一个xx-Bridging- ...
- vtk基础编程(2)-读取数据文件中的坐标点
原文地址: http://blog.csdn.net/chinamming/article/details/16860051 1. 案例说明 在实际计算中,常常需要大量的数据, 这个时候数据文件就必不 ...
- Ubuntu 安装Matlab2010a
1.挂载ISO 2.到/media/iso内,在终端执行./install 3.可视化安装 4.问题 1)/usr/local/MATLAB/R2010a/bin/util/oscheck.sh:/l ...
- 利用WebClient上传参数及文件流到远程ashx服务
原文 利用WebClient上传参数及文件流到远程ashx服务 1 思路: WebClient.UploadFile()方法可以上传文件:UploadData()方法可以上传数据参数:如何合二为一既上 ...
- eclipse中使用maven插件的时候,运行run as maven build的时候报错
-Dmaven.multiModuleProjectDirectory system propery is not set. Check $M2_HOME environment variable a ...
- 京JS 2013 - A two-day conference in Beijing for the JavaScript and Node.js community
京JS 2013 - A two-day conference in Beijing for the JavaScript and Node.js community 关于技术大会 京JS 2013 ...