(C/C++学习笔记)Copy构造函数应用场景

Copy构造函数应用场景

#include"iostream"
using namespace std;

class Location
{
private:
    int X,Y;
public:

    Location(int xx = 0 , int yy = 0)
    {
        X = xx;
        Y = yy;
        cout << "Constructor Object . \n";
    }
    Location(const Location &p)
    {
        X = p.x;
        Y = p.y;
        cout << "Copy_constructor called ."<< endl;
    }
    ~Location()
    {
        cout << X << "," << Y << " Object destroyed. "<< endl;
    }
    int GetX()
    {
        return X;
    }

    int GetY()
    {
        return Y;
    }
};

void f (Location p)
{
    cout << "Function:" << p.Getx() << "," << p.GetY() << endl;

}

void playobjmain()
{
    Location A(1,2);
    f(A);
}

int main(int argc, char const *argv[])
{
    playobjmain();
    return 0;
}

你可能感兴趣的:(C/C++)