C++类(二):C++友元函数和拷贝构造函数一起用时的问题

2 篇文章 0 订阅

本人初学C++,碰到一个问题,友元函数和拷贝构造函数共存情况下,若拷贝构造函数定义中不写继承成员变量的代码,实例化类的对象后,无法通过友元函数正常打印成员变量。
解决:后面同学给出了方案,通过引用作为参数,避免在传参过程中创建参数副本,即没有进入拷贝构造函数。

  1. 无拷贝构造函数情况下:
    友元函数可以正常访问私有成员变量,代码和输出如下:

代码:

    #include "pch.h"
    #include <iostream>
    using namespace std;
    class STU {
    public:
    	int ID;
    	float score;
    	int cnt;
    	STU(int x, float y);	/*构造函数*/
    	~STU(void);				/*析构函数	*/
    	friend void MsgPrint(STU stu);
    private:
    	int num;
    	int cars;
    	double money;
    };
    STU::STU(int x, float y)
    {
    	num = 11;
    	cnt = 0;
    	cars = 3;
    	money = 20.6f;
    	ID = x;
    	score = y;
    	cout << "Object is being created" << endl;
    };
    STU::~STU(void)
    {
    	cout << "Object is being deleted" << endl;
    }
    void MsgPrint(STU stu)
    {
    	printf("cars:%d\n", stu.cars);
    	printf("money:%f\n", stu.money);
    	printf("num:%d\n", stu.num);
    }
    int main()
    {
    	STU hm(2, 6);
    	MsgPrint(hm);
    }

输出:

Object is being created
cars:3
money:20.600000
num:11
Object is being deleted
Object is being deleted
G:\C++\Runoob\Lesson1_1\lesson1_1\Debug\lesson1_1.exe (进程11640)已退出,返回代 码为: 0。

  1. 友元函数和拷贝构造函数共存情况下:

代码:

#include "pch.h"
#include <iostream>
using namespace std;
class STU {
public:
	int ID;
	float score;
	int cnt;
	STU(int x, float y);	/*构造函数*/
	STU(const STU &obj);	/*拷贝构造函数*/
	~STU(void);				/*析构函数	*/
	friend void MsgPrint(STU stu);
private:
	int num;
	int cars;
	double money;
};
STU::STU(int x, float y)
{
	num = 11;
	cnt = 0;
	cars = 3;
	money = 20.6f;
	ID = x;
	score = y;
	cout << "Object is being created" << endl;
};
STU::STU(const STU &obj)
{
	cout << "调用拷贝构造函数" << endl;
	//cars = obj.cars;   /*注意这部分代码*/
	//num = obj.num;     /*注意这部分代码*/
	//money = obj.money; /*注意这部分代码*/
}
STU::~STU(void)
{
	cout << "Object is being deleted" << endl;
}
void MsgPrint(STU stu)
{
	printf("cars:%d\n", stu.cars);
	printf("money:%f\n", stu.money);
	printf("num:%d\n", stu.num);
}
int main()
{
	STU hm(2, 6);
	MsgPrint(hm);
}

31-33行注释情况下的输出,不正确:

Object is being created
调用拷贝构造函数
cars:2358696
money:8192.000000
num:20914556
Object is being deleted
Object is being deleted
G:\C++\Runoob\Lesson1_1\lesson1_1\Debug\lesson1_1.exe (进程11712)已退出,返回代 码为: 0。

31-33行不注释情况下的输出,正确:

Object is being created
调用拷贝构造函数
cars:3
money:20.6
num:11
Object is being deleted
Object is being deleted
G:\C++\Runoob\Lesson1_1\lesson1_1\Debug\lesson1_1.exe (进程10044)已退出,返回代 码为: 0。

  1. 解决:引用作为参数
  #include "pch.h"
  #include <iostream>
  using namespace std;
  class STU {
  public:
  	int ID;
  	float score;
  	int cnt;
  	STU(int x, float y);	/*构造函数*/
  	STU(const STU &obj);	/*拷贝构造函数*/
  	~STU(void);				/*析构函数	*/
  	friend void MsgPrint(STU& stu);
  
  private:
  	int num;
  	int cars;
  	double money;
  };
  STU::STU(int x, float y)
  {
  	num = 11;
  	cnt = 0;
  	cars = 3;
  	money = 20.6f;
  	ID = x;
  	score = y;
  	cout << "Object is being created" << endl;
  };
  STU::STU(const STU &obj)
  {
  	cout << "调用拷贝构造函数" << endl;
  	//cars = obj.cars;
  	//num = obj.num;
  	//money = obj.money;
  }
  STU::~STU(void)
  {
  	cout << "Object is being deleted" << endl;
  }
  void MsgPrint(STU& stu)
  {
  	printf("cars:%d\n", stu.cars);
  	printf("money:%f\n", stu.money);
  	printf("num:%d\n", stu.num);
  }
  int main()
  {
  	STU hm(2, 6);
  	MsgPrint(hm);
  }

输出:

Object is being created
调用拷贝构造函数
cars:3
money:20.600000
num:11
Object is being deleted
Object is being deleted
G:\C++\Runoob\Lesson1_1\lesson1_1\Debug\lesson1_1.exe (进程11656)已退出,返回代 码为: 0。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值