请解释"函数1(p1,p2,p3);"的输出

Please explain output from the `function1(p1,p2,p3);`

本文关键字:quot p2 p3 输出 解释 函数 p1      更新时间:2023-10-16
#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 coordinatet" << x << " " << y << endl;
}
coordinate(int a, int b)//PARAMETRIZED CONSTRUCTOR
{
x = a;
y = b;
cout << "prmtzd C coordinatet" << x << " " << y << endl;
}
coordinate(const coordinate &cpy)//COPY CONSTRUCTOR
{
x = cpy.x;
y = cpy.y;
cout << "CPY C coordinatet" << 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 coordinatet" << x << " " << y << endl;
}
};
///////////////////////////////////////////////////////////////////////////////////////////////////
class point 
{
private:
coordinate xy;
int z;
public:
coordinate &ref = xy;
point():xy(),z(0)//DEFAULT CONSTRUCTOR
{
cout << "def C pointt" << xy.getx() << " " << xy.gety() << " " << z << endl;
}
point(int a, int b, int c) :xy(a, b), z(c)
{
cout << "prmtzd C pointt" << xy.getx() << " " << xy.gety() << " " << z << endl;
}
point(coordinate a, int b)
{
xy = a;//this calls default constructor
z = b;
cout << "prmtzd C pointt" << xy.getx() << " " << xy.gety() << " " << z << endl;
}
point(const coordinate &cpy)//COPY CONSTRUCTOR
{
xy = cpy;
z = 100;
cout << "cpy C pointt" << 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 pointt" << 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;
}

我需要知道如何工作功能1function1(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时,p2p3将由编译器生成的默认副本构造函数构造。这将调用coordinate的默认复制构造函数来复制xy成员。这就是为什么您会得到两条CPY C coordinate输出线。(顺便说一句,新构建的pointref成员将指源对象的xy成员,而不是新构建的对象中的成员。(

如果您调用function1(p1,p2.xy,p3.xy);,那么您将看到您期望的cpy C point,因为参数points将由p2p3coordinates构造而成。