子类拷贝构造函数的问题【拜托各位看下】

mabo321 2009-01-17 11:55:05

class Prototype
{
public:
Prototype(string st)
{
str = st;
}
string Get()
{
return str;
}

virtual Prototype * Clone()=0;
virtual ~Prototype(){}
private:
string str;
};


class ConcretePrototype1 : public Prototype
{
public:
ConcretePrototype1(string st) : Prototype(st)
{
}
ConcretePrototype1(const ConcretePrototype1 & proto) // 就这块,提示父类Prototype没有默认构造。
{
member = proto.member;
cout << " ConcretePrototype1 Copy ... " <<endl;
}
Prototype * Clone()
{
member = new ConcretePrototype1(*this);
return member;
}

~ConcretePrototype1()
{}
private:
ConcretePrototype1 * member;
};


如果我在 父类Prototype中加一个默认构造 ,那么 Clone()函数拷贝出来的对象 的Get()值是空的啊……(可是我想的是 连父类的属性一起拷贝啊)

小弟我应该怎么弄啊……各位大虾,拜托指点一二……不胜感激
...全文
299 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
mabo321 2009-01-18
  • 打赏
  • 举报
回复

class Prototype
{
public:
Prototype(string st)
{
str = st;
}
string Get()
{
return str;
}

virtual Prototype * Clone()=0;
virtual ~Prototype(){}
private:
string str;
};


class ConcretePrototype1 : public Prototype
{
public:
ConcretePrototype1(string st) : Prototype(st)
{
}
ConcretePrototype1(ConcretePrototype1 & proto) : Prototype(proto.Get()) // 这样可以,我脑子唬住了0_o (星雨大哥的不能访问私有成员)
{
member = proto.member;
cout << " ConcretePrototype1 Copy ... " <<endl;
}
Prototype * Clone()
{
member = new ConcretePrototype1(*this);
return member;
}

~ConcretePrototype1()
{}
private:
ConcretePrototype1 * member;
};



谢谢各位……
OenAuth.Core 2009-01-18
  • 打赏
  • 举报
回复
晕,一下子卡到8楼来了,

呵呵,这种编译器会自己提示错误的,不过你要是自己加了一个默认的构造,如
Prototype()
{
str ="";
}
那就麻烦了,呵呵。
xiaoyisnail 2009-01-18
  • 打赏
  • 举报
回复

#include <string>
#include <iostream>
using namespace std;

class Prototype
{
public:
Prototype():str(""){}//或者这样为父类提供一个默认构造函数
Prototype(string st)
{
str = st;
}
string Get()
{
return str;
}

virtual Prototype * Clone()=0;
virtual ~Prototype(){}
private:
string str;
};


class ConcretePrototype1 : public Prototype
{
public:
ConcretePrototype1(string st) : Prototype(st)
{
}
ConcretePrototype1(const ConcretePrototype1 & proto)
{
member = proto.member;
cout << " ConcretePrototype1 Copy ... " <<endl;
}
Prototype * Clone()
{
member = new ConcretePrototype1(*this);
return member;
}

~ConcretePrototype1()
{}
private:
ConcretePrototype1 * member;
};
xiaoyisnail 2009-01-18
  • 打赏
  • 举报
回复

#include <string>
#include <iostream>
using namespace std;

class Prototype
{
public:
Prototype(string st)
{
str = st;
}
string Get()
{
return str;
}

virtual Prototype * Clone()=0;
virtual ~Prototype(){}
private:
string str;
};


class ConcretePrototype1 : public Prototype
{
public:
ConcretePrototype1(string st) : Prototype(st)
{
}
ConcretePrototype1(const ConcretePrototype1 & proto):Prototype("") //可以这样用空串来初始化
{
member = proto.member;
cout << " ConcretePrototype1 Copy ... " <<endl;
}
Prototype * Clone()
{
member = new ConcretePrototype1(*this);
return member;
}

~ConcretePrototype1()
{}
private:
ConcretePrototype1 * member;
};
OenAuth.Core 2009-01-18
  • 打赏
  • 举报
回复
拷贝构造也是构造函数,应该采用初始化成员列表的形式把父类的也加上
xiaoyisnail 2009-01-18
  • 打赏
  • 举报
回复
子类如果没有显示调用父类的构造函数初始化父类部分的话,编译器会尝试调用父类的默认构造函数来初始化父类部分,由于你没有为父类提供默认构造函数,所以错了
oyljerry 2009-01-18
  • 打赏
  • 举报
回复
初始化列表,把str初始化你的父类
malganis00 2009-01-18
  • 打赏
  • 举报
回复
认真看一下书
!!
OenAuth.Core 2009-01-18
  • 打赏
  • 举报
回复
星羽哥,飞雪兄,该睡觉啦
waizqfor 2009-01-18
  • 打赏
  • 举报
回复

class Prototype
{
public:
Prototype(string st)
{
str = st;
}
string Get()
{
return str;
}

virtual Prototype * Clone()=0;
virtual ~Prototype(){}
private:
string str;
};


class ConcretePrototype1 : public Prototype
{
public:
ConcretePrototype1(string st) : Prototype(st)
{
}
ConcretePrototype1(const ConcretePrototype1 & proto):Prototype(st) // 就这块,提示父类Prototype没有默认构造。
{
member = proto.member;
cout << " ConcretePrototype1 Copy ... " <<endl;
}
Prototype * Clone()
{
member = new ConcretePrototype1(*this);
return member;
}

~ConcretePrototype1()
{}
private:
ConcretePrototype1 * member;
};
baihacker 2009-01-18
  • 打赏
  • 举报
回复
ConcretePrototype1(const ConcretePrototype1 & proto) 要和
ConcretePrototype1(string st) : Prototype(st)
类似处理一下
星羽 2009-01-18
  • 打赏
  • 举报
回复
class Prototype
{
public:
Prototype(string st)
{
str = st;
}
string Get()
{
return str;
}

virtual Prototype * Clone()=0;
virtual ~Prototype(){}
protected:
string str;
};


class ConcretePrototype1 : public Prototype
{
public:
ConcretePrototype1(string st) : Prototype(st)
{
}
ConcretePrototype1(const ConcretePrototype1 & proto) // 就这块,提示父类Prototype没有默认构造。
: Prototype(proto.str)
{
member = proto.member;
cout << " ConcretePrototype1 Copy ... " <<endl;
}
Prototype * Clone()
{
member = new ConcretePrototype1(*this);
return member;
}

~ConcretePrototype1()
{}
private:
ConcretePrototype1 * member;
};

64,648

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧