克隆C ++对象 [英] Cloning C++ objects
问题描述
我正试图以某种方式实现多态对象克隆(就像它使用Java中的
),但是当我写道:
class Object {
public:
virtual Object * clone()const = 0;
// ...
}
class String:public Object {
public:
Object * clone()const {return new String(* this); }
// ...
}
编译器告诉我,我不能分配类String的对象,
因为某些方法是纯粹的。在
Object中还有一些虚拟方法,但是都在String中实现。我该怎么做才能解决这个问题?
Paul PAZABO Zaborski
Hi,
I''m trying to somehow implement polymorphic object cloning (just as it
is in Java), but when I write:
class Object {
public:
virtual Object * clone() const = 0;
// ...
}
class String : public Object {
public:
Object * clone() const { return new String(*this); }
// ...
}
compiler tells me, that I cannot allocate object of class String,
because some methods are "pure". There are some more virtual methods in
Object, but there are all implemented in String. What should I do to
solve this problem?
Paul PAZABO Zaborski
推荐答案
paz ... @ gmail.com写道:
paz...@gmail.com wrote:
我''我试图以某种方式实现多态对象克隆(就像它使用Java中的
),但是当我写道:
class Object {
public:
虚拟对象* clone()const = 0;
// ...
}
class String:public Object {
public:
Object * clone()const {return new String(* this); }
// ...
}
编译器告诉我,我不能分配类String的对象,
因为某些方法是纯粹的。在
Object中还有一些虚拟方法,但是都在String中实现。我该怎么办?
解决这个问题?
Hi,
I''m trying to somehow implement polymorphic object cloning (just as it
is in Java), but when I write:
class Object {
public:
virtual Object * clone() const = 0;
// ...
}
class String : public Object {
public:
Object * clone() const { return new String(*this); }
// ...
}
compiler tells me, that I cannot allocate object of class String,
because some methods are "pure". There are some more virtual methods in
Object, but there are all implemented in String. What should I do to
solve this problem?
很可能你忘了从Object实现一些东西(或者你在String中添加了一个纯虚函数的
)。发布更多代码,我们
可能会告诉你问题是什么。
干杯! --M
It''s likely that you forgot to implement something from Object (or that
you added a pure virtual function in String). Post more code, and we
can probably tell you what the problem is.
Cheers! --M
mlimber写道:
mlimber wrote:
paz ... @ gmail.com写道:
paz...@gmail.com wrote:
我正试图以某种方式实现多态对象克隆(就像它是
是用Java编写的),但是当我写的时候:
class Object {
public:
虚拟对象* clone()const = 0;
// ...
}
class字符串:public Object {
public:
Object * clone()const {return new String(* this); }
// ...
}
编译器告诉我,我不能分配类String的对象,
因为某些方法是纯粹的。在
Object中还有一些虚拟方法,但是都在String中实现。我该怎么办?
解决这个问题?
Hi,
I''m trying to somehow implement polymorphic object cloning (just as it
is in Java), but when I write:
class Object {
public:
virtual Object * clone() const = 0;
// ...
}
class String : public Object {
public:
Object * clone() const { return new String(*this); }
// ...
}
compiler tells me, that I cannot allocate object of class String,
because some methods are "pure". There are some more virtual methods in
Object, but there are all implemented in String. What should I do to
solve this problem?
很可能你忘了从Object实现一些东西(或者你在String中添加了一个纯虚函数的
)。发布更多代码,我们
可能会告诉你问题是什么。
干杯! --M
It''s likely that you forgot to implement something from Object (or that
you added a pure virtual function in String). Post more code, and we
can probably tell you what the problem is.
Cheers! --M
哦..我看到了问题..我把const在Object中的一些方法之后,
但我忘了用String做,所以编译器认为它是抽象的
类。
无论如何,谢谢求助:)
Oh.. I see the problem.. I put "const" after some methods in Object,
but I forgot to do it in String, so compiler considered it as abstract
class.
Anyway, thanks for help :)
pa****@gmail.com schrieb:
pa****@gmail.com schrieb:
我试图以某种方式实现多态对象克隆(就像它是
是Java),但是当我写的时候:
class Object {
public:
虚拟对象* clone()const = 0;
// ...
}
类字符串:public对象{
public:
Object * clone()const {return new String(* this); }
Hi,
I''m trying to somehow implement polymorphic object cloning (just as it
is in Java), but when I write:
class Object {
public:
virtual Object * clone() const = 0;
// ...
}
class String : public Object {
public:
Object * clone() const { return new String(*this); }
我会在这里返回一个String *:
String * clone()const {return new String(* this ); }
所以你可以克隆一个字符串并获得一个字符串而不进行强制转换。
I would return a String* here:
String* clone() const { return new String(*this); }
So you can clone a String and get a String without casting.
// ... ...
}
// ...
}
-
Thomas
http://www.netmeister.org/news/learn2quote.html
这篇关于克隆C ++对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!