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