首页 > 解决方案 > 请解释 `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

标签: c++

解决方案


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你所期待的,因为参数points 将由and的coordinates 构成。p2p3


推荐阅读