返回信息流有一道C++改错题,主要是实现对象的复制然后打印,自己在DEV C++上总是改不对,求大神指教
#include <iostream>
// No other “#include” statements are permitted.
void AllocateAndCopy (char *destination, const char* source)
{
destination = new char[strlen(source)];
strcpy(destination, source); //to be corrected, no <string> required?
}
class Test
{
char* p;
Test(const char* source)
{
AllocateAndCopy(p, source); //to be corrected, p pointer is not expected?
}
~Test()
{
delete p; //if no "new" in "AllocateAndCopy", no need "delete"?
}
void Print()
{
std::cout << p;
}
};
void TestFunction()
{
Test c("Testing");
Test d = c; // need object copying function?
d.Print();
}
main()
{
TestFunction();
}
这是一条镜像帖。来源:北邮人论坛 / cpp / #92554同步于 2016/7/14
该镜像源已超过 30 天没有更新,可能在源站已被删除。
CPP机器人发帖
[问题]求教大神一道C++对象复制的问题
Michaeling
2016/7/14镜像同步23 回复
订阅后,新回复会通过你的通知中心匿名送达。
9 条回复
【 在 Michaeling 的大作中提到: 】
: 有一道C++改错题,主要是实现对象的复制然后打印,自己在DEV C++上总是改不对,求大神指教
: #include <iostream>
: // No other “#include” statements are permitted.
: ...................
#include <iostream>
// No other “#include” statements are permitted.
void AllocateAndCopy(char* &destination, const char* source)
{
destination = new char[strlen(source) + 1];
strcpy(destination, source); //to be corrected, no <string> required?
}
class Test
{
public:
char* p;
Test(const char* source)
{
AllocateAndCopy(p, source); //to be corrected, p pointer is not expected?
}
~Test()
{
delete p; //if no "new" in "AllocateAndCopy", no need "delete"?
}
void Print()
{
std::cout << p;
}
};
void TestFunction()
{
Test c("Testing");
Test d = c.p; // need object copying function?
d.Print();
}
int main()
{
TestFunction();
}
只对程序做最少改动,不保证其它。
【 在 n1 的大作中提到: 】
: 两个问题,一是注意strlen获取的字符串长度不包括\0,所以你申请的数组长度得加一
: 第二是Test d=c,缺少拷贝构造函数,默认是浅拷贝所以你需要自己写一个
补充一下第二条,Test默认合成的拷贝构造函数,只是将char* p复制过去,并没有动态开辟内存,c和d的指针p指向同一块内存,当执行完TestFunction()后,c和d执行析构函数,相同的地址会被delete两遍。
另外,1. class默认访问权限是private,构造函数,析构函数,打印函数都不能调用, 需要添加访问说明符public:
2. strlen, strcpy需要string.h头文件
【 在 Michaeling 的大作中提到: 】
: 有一道C++改错题,主要是实现对象的复制然后打印,自己在DEV C++上总是改不对,求大神指教
: #include <iostream>
: // No other “#include” statements are permitted.
: ...................
复制分三种情况:一是初始化复制构造,二是实参传递形参,三是返回值
额,虽然不是太礼貌,但是还是想问一下:
2楼和3楼真的明白,什么叫赋值,什么叫初始化嘛?
Test d = c.p; // 这个东西是调用复制构造函数? 我读书少,别骗我啦,贵邮的大神们
【 在 zx723 的大作中提到: 】
: 额,虽然不是太礼貌,但是还是想问一下:
: 2楼和3楼真的明白,什么叫赋值,什么叫初始化嘛?
:
: ...................
首先题目说改错,意思是能运行不出错就行,并没有说一定要用拷贝构造函数;其次我已经说了只改动最少,不保证其它。
你要的拷贝构造函数。
#include <iostream>
void AllocateAndCopy(char* &destination, const char* source) {
destination = new char[strlen(source) + 1];
strcpy(destination, source);
}
class Test {
public:
Test(const Test& other) : p(nullptr) {
AllocateAndCopy(p, other.p);
}
Test(const char* source) {
AllocateAndCopy(p, source);
}
~Test() {
if (p)
delete p;
}
void Print() {
std::cout << p;
}
private:
char* p;
};
void TestFunction() {
Test c("Testing");
Test d = c;
d.Print();
}
int main() {
TestFunction();
}
【 在 fengyiqiao 的大作中提到: 】
: 首先题目说改错,意思是能运行不出错就行,并没有说一定要用拷贝构造函数;其次我已经说了只改动最少,不保证其它。
: 你要的拷贝构造函数。
: [code=c]
: ...................
额,好吧。
首先,你其实是沙发。。。也就是0楼。。。
其次,我也觉得完成楼主的要求并不需要拷贝构造函数。。。
再次,我也不要拷贝构造函数。。。
最后,感谢贵邮大神热心给我一个拷贝构造函数
【 在 zx723 的大作中提到: 】
:
: 额,好吧。
: 首先,你其实是沙发。。。也就是0楼。。。
: ...................
那我曲解你的意思了,我只是觉得你的回复不太友好,论坛和谐为主。