c++ - 请解释 `function1(p1,p2,p3);` 的输出
问题描述
#include <iostream>
#include <iomanip>
#include <string>
#include <math.h>
#include <cmath>
#include <cstdlib>
#include <vector>
#include <ctime>
#include <algorithm>
using namespace std;
class coordinate
{
private:
int x;
int y;
public:
coordinate()//DEFAULT CONSTRUCTOR
{
x = 0;
y = 0;
cout << "def C coordinate\t" << x << " " << y << endl;
}
coordinate(int a, int b)//PARAMETRIZED CONSTRUCTOR
{
x = a;
y = b;
cout << "prmtzd C coordinate\t" << x << " " << y << endl;
}
coordinate(const coordinate &cpy)//COPY CONSTRUCTOR
{
x = cpy.x;
y = cpy.y;
cout << "CPY C coordinate\t" << x << " " << y << endl;
}
void setall(int a, int b){x = a; y = b;}
int getx(){return x;}
int gety(){return y;}
~coordinate()//DESTRUCTOR
{
cout << "D coordinate\t" << x << " " << y << endl;
}
};
///////////////////////////////////////////////////////////////////////////////////////////////////
class point
{
private:
coordinate xy;
int z;
public:
coordinate &ref = xy;
point():xy(),z(0)//DEFAULT CONSTRUCTOR
{
cout << "def C point\t" << xy.getx() << " " << xy.gety() << " " << z << endl;
}
point(int a, int b, int c) :xy(a, b), z(c)
{
cout << "prmtzd C point\t" << xy.getx() << " " << xy.gety() << " " << z << endl;
}
point(coordinate a, int b)
{
xy = a;//this calls default constructor
z = b;
cout << "prmtzd C point\t" << xy.getx() << " " << xy.gety() << " " << z << endl;
}
point(const coordinate &cpy)//COPY CONSTRUCTOR
{
xy = cpy;
z = 100;
cout << "cpy C point\t" << xy.getx() << " " << xy.gety() << " " << z << endl;
}
void setall(int a, int b, int c){xy.setall(a, b); z = c;}
void print() { cout << xy.getx() << " " << xy.gety() << " " << z << endl; }
~point()//DESTRUCTOR
{
cout << "D point\t" << xy.getx() << " " << xy.gety() << " " << z << endl;
}
};
void function1(point &p1, point p2, point p3)
{
p2.setall(10, 10, 10);
p3.ref.setall(200, 200);
}
coordinate co1;
int main()
{
point p1;
co1.setall(100, 100);
point p2(5, 5, 5);
point p3(co1);
p1.print();
p2.print();
p3.print();
**function1(p1,p2,p3);**
co1.setall(8, 8);
point p4(co1, 8);
co1.~coordinate();
p1.print();
p2.print();
p3.print();
return 0;
}
我需要知道如何工作功能 1 function1(p1,p2,p3);
输出是
CPY C coordinate 100 100??
CPY C coordinate 5 5 ??
为什么不打印
cpy C point
输出
def C coordinate 0 0
def C coordinate 0 0
def C point 0 0 0
prmtzd C coordinate 5 5
prmtzd C point 5 5 5
def C coordinate 0 0
cpy C point 100 100 100
0 0 0
5 5 5
100 100 100
CPY C coordinate 100 100
CPY C coordinate 5 5
D point 10 10 10
D coordinate 10 10
D point 100 100 100
D coordinate 100 100
CPY C coordinate 8 8
def C coordinate 0 0
prmtzd C point 8 8 8
D coordinate 8 8
D coordinate 8 8
0 0 0
5 5 5
200 200 100
D point 8 8 8
D coordinate 8 8
D point 200 200 100
D coordinate 200 200
D point 5 5 5
D coordinate 5 5
D point 0 0 0
D coordinate 0 0
D coordinate 8 8
解决方案
point(const coordinate &cpy)
不是复制构造函数。您还没有为point
. 当function1
被调用p2
并将p3
由编译器生成的默认复制构造函数构造。这将调用默认的复制构造函数coordinate
来复制xy
成员。这就是为什么你得到两条CPY C coordinate
输出线的原因。(顺便说一句,ref
新构造point
的成员将引用xy
源对象的成员,而不是新构造的对象中的成员。)
如果你打电话function1(p1,p2.xy,p3.xy);
,那么你会看到cpy C point
你所期待的,因为参数point
s 将由and的coordinate
s 构成。p2
p3
推荐阅读
- javascript - 为什么我的 firebase 云功能会运行多次?
- c++ - C 8.2.1 狂想曲开发者
- excel - FoxitPhantom pdf 转 Excel 自动化
- php - 居中 HTML 对象 - 这包括标签、文本输入框和按钮?
- crm - 如何在 Zoho CRM 中自动填写字段
- arrays - React - 数组作为它们的长度传递
- schema - 如何在 OpenApi 中使用 OneOf 和两个类似的模式?
- java - FusedLocationProviderClient 返回错误的经纬度
- r - for循环:无法将数据帧存储在列表中
- sql - 记录满足 2 个变量的计数