#include iostreamusing namespace std;class TestpublicTestint xvalue=xcoutvalue=valueendl;构造函数被调用TestTest &tvalue=tvalue;cout复制构造函数被调用value-valueendl;~Testvoidcout析构函数被调用value=valueendl;int GetValueOvo
The code has syntax errors and won't compile. Here is a corrected version:
#include
class Test { public: Test(int x) { value = x; cout << "value=" << value << endl; } Test(Test &t) { value = t.value; cout << "复制构造函数被调用,value=" << value << endl; } ~Test(void) { cout << "析构函数被调用,value=" << value << endl; } int GetValue() { return value; } void SetValue(int x) { value = x; } private: int value; };
void fun(Test t) { t.SetValue(20); cout << t.GetValue() << endl; }
int main() { Test a(5); fun(a); return 0; }
The code defines a class Test with a constructor that sets an integer value and prints it, a copy constructor that copies the value from another Test object and prints it, a destructor that prints the value, and two member functions for getting and setting the value. The code also defines a function fun that takes a Test object by value, sets its value to 20, and prints it. In main, a Test object a is created with value 5, and fun is called with a as argument. The output should be:
value=5 value=20 20 析构函数被调用,value=20 析构函数被调用,value=
原文地址: http://www.cveoy.top/t/topic/fgXV 著作权归作者所有。请勿转载和采集!