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

mabo321 2009-01-17 11:55:12

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()值是空的啊……(可是我想的是 连父类的属性一起拷贝啊)

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

飞雪同志是个好同志啊……
mabo321 2009-01-18
  • 打赏
  • 举报
回复
5,6楼的,也可以,
不过就是 没有 原始的Clone下来……
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;
};



谢谢 各位……
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;
};
oyljerry 2009-01-18
  • 打赏
  • 举报
回复
初始化列表,把str初始化你的父类
waizqfor 2009-01-18
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 baihacker 的回复:]
class Prototype
{
public:
Prototype(string st)
{
str = st;
}
string Get() const //const
{
return str;
}

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


class ConcretePrototype1 : public Prototype
{
public:
ConcretePrototype1(string st) : Prototype(st)
{
} …
[/Quote]
UP
星羽 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;
};

baihacker 2009-01-18
  • 打赏
  • 举报
回复
class Prototype
{
public:
Prototype(string st)
{
str = st;
}
string Get() const //const
{
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( proto.Get()) //调用一下父类的 {
member = proto.member;
cout << " ConcretePrototype1 Copy ... " <<endl;
}
Prototype * Clone()
{
member = new ConcretePrototype1(*this);
return member;
}

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

65,211

社区成员

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

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