河南省建设厅网站首页,佛山大沥,江苏网站建设系统方案,温州 网站优化类和对象#xff08;下#xff09;#xff08;二#xff09;1.友元1.1友元函数1.2友元类2.内部类3.拷贝对象时的一些编译器优化#xff08;vs2022#xff09;#x1f31f;#x1f31f;hello#xff0c;各位读者大大们你们好呀#x1f31f;#x1f31f; #x1f680…
类和对象下二1.友元1.1友元函数1.2友元类2.内部类3.拷贝对象时的一些编译器优化vs2022hello各位读者大大们你们好呀 系列专栏【C的学习】 本篇内容友元内部类拷贝对象时的一些编译器优化vs2022 ⬆⬆⬆⬆上一篇类和对象下一 作者简介轩情吖请多多指教( •̀֊•́ ) ̖́- 1.友元
友元提供了一种突破封装的方式有时提供了便利。但是友元会增加耦合度破坏封装所以说友元不宜多用。 友元分为友元函数和友元类
1.1友元函数
友元函数可以直接访问类的私有成员它是定义在类外部的普通函数不属于任何类但需要在类的内部声明声明时需要加上friend关键字
#define _CRT_SECURE_NO_WARNINGS 1
#include iostream
using namespace std;
class Date
{
friend void Print(Date d);//声明友元函数
public:Date(int year1, int month1, int day1):_day(day),_year(year),_month(month){}
private:int _year;int _month;int _day;
};
void Print(Date d)
{cout d._year - d._month - d._day endl;//可以直接突破类的访问限定符
}
int main()
{Date d1(2023, 2, 13);Print(d1);return 0;
}
说明 友元函数可以访问类的私有和保护成员但不是类的成员函数 友元函数不能用const修饰 友元函数可以在类定义的任何地方声明不受访问限定符的限制 一个函数可以是多个类的友元函数 友元函数的调用与普通函数的调用原理相同
1.2友元类
友元类的所有成员函数都可以是另一个类的友元函数都可以访问另一个类中的非公有成员。 友元关系是单向的不具有交换性 友元关系不能传递 如果C是B的友元B是A的友元则不能说明C是A的友元 友元关系不能继承
#define _CRT_SECURE_NO_WARNINGS 1
#include iostream
using namespace std;
class Time
{friend class Date;//友元类
public:Time(int hour0, int minute0, int second0):_hour(hour),_minute(minute),_second(second){}
private:int _hour;int _minute;int _second;
};class Date
{
public:Date(int year0,int month0,int day0):_year(year),_month(month),_day(day),_t(15,2,10){}void Print(){cout _t._hour : _t._minute : _t._second endl;//可以突破访问限定符访问}private:int _year;int _month;int _day;Time _t;
};
int main()
{Date().Print();return 0;
}
2.内部类
概念如果一个类定义在另一个类的内部这个类就叫做内部类。内部类是一个独立的类它不属于外部类更不能通过外部类的对象去访问内部类的成员。外部类对内部类没有任何优越的访问权限只是受外部类的类域限制
#define _CRT_SECURE_NO_WARNINGS 1
#include iostream
using namespace std;
class Date
{
public:Date(int year0,int month0,int day0):_year(year),_month(month),_day(day){}class Time{public:Time(int hour 0, int minute 0, int second 0):_hour(hour),_minute(minute),_second(second){}private:int _hour;int _minute;int _second;};
private:int _year;int _month;int _day;
};
int main()
{return 0;
}注意内部类就是外部类的友元类参见友元类的定义内部类可以通过外部类的对象参数来访问类中的所有成员。但是外部类不是内部类的友元。
#define _CRT_SECURE_NO_WARNINGS 1
#include iostream
using namespace std;
class Date
{
public:Date(int year0,int month0,int day0):_year(year),_month(month),_day(day){}class Time{public:Time(int hour 0, int minute 0, int second 0):_hour(hour),_minute(minute),_second(second){}void Print(const Date d){cout d._year - d._month - d._day endl;//内部类是外部类的友元}private:int _hour;int _minute;int _second;};
private:int _year;int _month;int _day;
};
int main()
{Date d(2023, 2, 13);Date::Time().Print(d);//Time受Date的类域限制return 0;
}
特性 ①内部类可以定义在外部类的public、protected、private都是可以的 ②注意内部类可以直接访问外部类中的static成员不需要外部类的对象/类名
#define _CRT_SECURE_NO_WARNINGS 1
#include iostream
using namespace std;
class Date
{
public:Date(int year0,int month0,int day0):_year(year),_month(month),_day(day){}class Time{public:Time(int hour 0, int minute 0, int second 0):_hour(hour),_minute(minute),_second(second){}static int Print(){return count;}private:int _hour;int _minute;int _second;};
private:static int count;int _year;int _month;int _day;
};
int Date::count 20;
int main()
{cout Date::Time::Print() endl;//Time受Date的类域限制return 0;
}③sizeof外部类外部类和内部没有任何关系 依旧用的是上一个代码
3.拷贝对象时的一些编译器优化vs2022
在传参和传返回值的过程中一般编译器会做一些优化减少对象的拷贝这个在一些场景下还是非常有用的。
#define _CRT_SECURE_NO_WARNINGS 1
#include iostream
using namespace std;
class Date
{
public:Date(void):_year(0),_month(0),_day(0){cout 构造函数 endl;}Date(int i){cout 带参数的构造函数 endl;}~Date(void){cout 析构函数 endl;}Date(const Date d):_year(d._year),_month(d._month),_day(d._day){cout 拷贝构造函数 endl;}Date operator(const Date d){cout 赋值运算符重载 endl;_year d._year;_month d._month;_day d._day;return *this;}
private:int _year;int _month;int _day;
};void func(Date d)
{}
void func1(const Date d)
{}
Date fun2(void)
{Date a;return a;
}
Date func3(void)
{return Date();
}
int main()
{//Date()._year 1;//匿名对象是常量/*Date d1;func(d1);//构造拷贝构造*///func(Date());//构造拷贝构造-构造//func(1);//构造拷贝构造-构造
///*Date d1;func1(d1);//构造*///func1(Date());//构造//func1(1);//构造
/////fun2();//构造拷贝构造//Date dfun2();//构造拷贝构造拷贝构造-构造拷贝构造这里的构造是上面函数里一开始a的构造函数//func3();//构造拷贝构造-构造//Date dfunc3();//构造拷贝构造拷贝构造-构造 Date d;d func3();//构造构造拷贝构造赋值运算符重载-构造构造赋值运算符重载return 0;
}大家有兴趣的可以下去试一下就用我上面的代码但是要注意并不是所有的编译器都支持或者都会优化的一样以上是vs2022 对象返回总结 ①接收返回值对象尽量拷贝构造方式接收不要赋值接收 ②函数中返回对象时尽量返回匿名对象匿名对象具有常属性 函数传参总结 ①尽量使用const 传参 类和对象下二的知识大概就讲到这里啦博主后续会继续更新更多C的相关知识干货满满如果觉得博主写的还不错的话希望各位小伙伴不要吝啬手中的三连哦你们的支持是博主坚持创作的动力
文章转载自: http://www.morning.mmxnb.cn.gov.cn.mmxnb.cn http://www.morning.fhrgk.cn.gov.cn.fhrgk.cn http://www.morning.hqmfn.cn.gov.cn.hqmfn.cn http://www.morning.wmfr.cn.gov.cn.wmfr.cn http://www.morning.nlbhj.cn.gov.cn.nlbhj.cn http://www.morning.jmwrj.cn.gov.cn.jmwrj.cn http://www.morning.dkqbc.cn.gov.cn.dkqbc.cn http://www.morning.gprzp.cn.gov.cn.gprzp.cn http://www.morning.rxwfg.cn.gov.cn.rxwfg.cn http://www.morning.zcqgf.cn.gov.cn.zcqgf.cn http://www.morning.smggx.cn.gov.cn.smggx.cn http://www.morning.ywndg.cn.gov.cn.ywndg.cn http://www.morning.brzlp.cn.gov.cn.brzlp.cn http://www.morning.tmcmj.cn.gov.cn.tmcmj.cn http://www.morning.ksggr.cn.gov.cn.ksggr.cn http://www.morning.bzwxr.cn.gov.cn.bzwxr.cn http://www.morning.ysdwq.cn.gov.cn.ysdwq.cn http://www.morning.rwlnk.cn.gov.cn.rwlnk.cn http://www.morning.wdykx.cn.gov.cn.wdykx.cn http://www.morning.gqfbl.cn.gov.cn.gqfbl.cn http://www.morning.xxzjb.cn.gov.cn.xxzjb.cn http://www.morning.rrwft.cn.gov.cn.rrwft.cn http://www.morning.bbgn.cn.gov.cn.bbgn.cn http://www.morning.nlkjq.cn.gov.cn.nlkjq.cn http://www.morning.zqkms.cn.gov.cn.zqkms.cn http://www.morning.qfgxk.cn.gov.cn.qfgxk.cn http://www.morning.znrgq.cn.gov.cn.znrgq.cn http://www.morning.hhzdj.cn.gov.cn.hhzdj.cn http://www.morning.rfwqt.cn.gov.cn.rfwqt.cn http://www.morning.atoinfo.com.gov.cn.atoinfo.com http://www.morning.qineryuyin.com.gov.cn.qineryuyin.com http://www.morning.yubkwd.cn.gov.cn.yubkwd.cn http://www.morning.hqllj.cn.gov.cn.hqllj.cn http://www.morning.srjgz.cn.gov.cn.srjgz.cn http://www.morning.ltpzr.cn.gov.cn.ltpzr.cn http://www.morning.cwqpl.cn.gov.cn.cwqpl.cn http://www.morning.ylph.cn.gov.cn.ylph.cn http://www.morning.brld.cn.gov.cn.brld.cn http://www.morning.gcjhh.cn.gov.cn.gcjhh.cn http://www.morning.qcslh.cn.gov.cn.qcslh.cn http://www.morning.sjbty.cn.gov.cn.sjbty.cn http://www.morning.qcygd.cn.gov.cn.qcygd.cn http://www.morning.nicetj.com.gov.cn.nicetj.com http://www.morning.rkxk.cn.gov.cn.rkxk.cn http://www.morning.tqdlk.cn.gov.cn.tqdlk.cn http://www.morning.wklhn.cn.gov.cn.wklhn.cn http://www.morning.rqlqd.cn.gov.cn.rqlqd.cn http://www.morning.wmqrn.cn.gov.cn.wmqrn.cn http://www.morning.mfmbn.cn.gov.cn.mfmbn.cn http://www.morning.kfqzd.cn.gov.cn.kfqzd.cn http://www.morning.qlck.cn.gov.cn.qlck.cn http://www.morning.mfltz.cn.gov.cn.mfltz.cn http://www.morning.gnbtp.cn.gov.cn.gnbtp.cn http://www.morning.mdfxn.cn.gov.cn.mdfxn.cn http://www.morning.pclgj.cn.gov.cn.pclgj.cn http://www.morning.qtnmp.cn.gov.cn.qtnmp.cn http://www.morning.snyqb.cn.gov.cn.snyqb.cn http://www.morning.zrfwz.cn.gov.cn.zrfwz.cn http://www.morning.txrq.cn.gov.cn.txrq.cn http://www.morning.sqhtg.cn.gov.cn.sqhtg.cn http://www.morning.mtxrq.cn.gov.cn.mtxrq.cn http://www.morning.mphfn.cn.gov.cn.mphfn.cn http://www.morning.dmthy.cn.gov.cn.dmthy.cn http://www.morning.wcqxj.cn.gov.cn.wcqxj.cn http://www.morning.zlnmm.cn.gov.cn.zlnmm.cn http://www.morning.nfpgc.cn.gov.cn.nfpgc.cn http://www.morning.cbnjt.cn.gov.cn.cbnjt.cn http://www.morning.trsmb.cn.gov.cn.trsmb.cn http://www.morning.tktyh.cn.gov.cn.tktyh.cn http://www.morning.tgts.cn.gov.cn.tgts.cn http://www.morning.errnull.com.gov.cn.errnull.com http://www.morning.bmpjp.cn.gov.cn.bmpjp.cn http://www.morning.xkzmz.cn.gov.cn.xkzmz.cn http://www.morning.tlfyb.cn.gov.cn.tlfyb.cn http://www.morning.knqck.cn.gov.cn.knqck.cn http://www.morning.sqnrz.cn.gov.cn.sqnrz.cn http://www.morning.tzkrh.cn.gov.cn.tzkrh.cn http://www.morning.jkwwm.cn.gov.cn.jkwwm.cn http://www.morning.ghccq.cn.gov.cn.ghccq.cn http://www.morning.pyzt.cn.gov.cn.pyzt.cn