#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
答案 0 :(得分:0)
for line in file:
if 'somepattern' not in line:
continue
# if we got here, 'somepattern' is in the line, so process it
不是副本构造函数。您尚未提供point(const coordinate &cpy)
的副本构造函数。当point
被调用function1
时,p2
将由编译器生成的默认副本构造函数构造。这将调用p3
的默认复制构造函数来复制coordinate
成员。这就是为什么您要获得两条xy
输出线的原因。 (偶然地,新建的CPY C coordinate
的{{1}}成员将引用源对象的ref
成员,而不是新建的对象中的成员。)
如果您调用point
,则会看到您期望的xy
,因为参数function1(p1,p2.xy,p3.xy);
s将由{{ 1}}和cpy C point
。