复制对象

编程入门 行业动态 更新时间:2024-10-28 10:32:36
本文介绍了复制对象 - 保留多态性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

以下代码尝试复制对象并保留原始类型。 不幸的是它不工作(每个复制的对象将成为 Super ,而不是与它的原来的类相同)。

请注意, copySuper(const Super& givenSuper)不应该知道任何关于 Super 。

有可能做这样的副本吗?或者我必须更改 copySuper ?

的定义

#include < string> #include< iostream> class Super { public: Super(){}; virtual〜Super(){}; virtual std :: string toString()const { returnI'm Super! } }; class Special:public Super { public: Special(){}; virtual〜Special(){}; virtual std :: string toString()const { returnI'm Special! } }; Super * copySuper(const Super& givenSuper) { Super * superCopy(new Super(givenSuper)); return superCopy; } int main() {特殊; std :: cout<< special.toString()<< std :: endl; std :: cout<< ---<< std :: endl; Super * specialCopy = copySuper(special); std :: cout<< specialCopy-> toString()<< std :: endl; return 0; } //所需输出: //#我是特殊! //#--- //#我是特别的! // //实际输出: //#我是Sepcial! //#--- //#我是超级!

解决方案

尝试:

class Super { public: Super(); // regular ctor Super(const Super& _rhs); //复制构造函数 virtual Super * clone()const {return(new Super(* this));}; }; // eo class Super class特殊:public Super { public: Special():Super(){}; Special(const Special& _rhs):Super(_rhs){}; virtual Special * clone()const {return(new Special(* this));}; }; // eo class Special

注意,我们已经实现了一个clone

<$ c $

c> Super * s = new Super(); Super * s2 = s-> clone(); // copy of s Special * a = new Special(); Special * b = a-> clone(); //复制

编辑:正如其他评论员指出的, * this ,而不是此。

EDIT2:我真的不应该邮局这么快,当在中间工作。用于协变返回类型的Special :: clone()的修改返回类型。

The following code tries to copy an object and keep the original type. Unfortunately it does not work (every copied object will become a Super instead of being of the same class as its original).

Please note that copySuper(const Super& givenSuper) should not know anything about the subclasses of Super.

Is it possible to do such a copy? Or do I have to change the definition of copySuper ?

#include <string> #include <iostream> class Super { public: Super() {}; virtual ~Super() {}; virtual std::string toString() const { return "I'm Super!"; } }; class Special : public Super { public: Special() {}; virtual ~Special() {}; virtual std::string toString() const { return "I'm Special!"; } }; Super* copySuper(const Super& givenSuper) { Super* superCopy( new Super(givenSuper) ); return superCopy; } int main() { Special special; std::cout << special.toString() << std::endl; std::cout << "---" << std::endl; Super* specialCopy = copySuper(special); std::cout << specialCopy->toString() << std::endl; return 0; } //Desired Output: // # I'm Special! // # --- // # I'm Special! // //Actual Output: // # I'm Sepcial! // # --- // # I'm Super!

解决方案

Try this:

class Super { public: Super();// regular ctor Super(const Super& _rhs); // copy constructor virtual Super* clone() const {return(new Super(*this));}; }; // eo class Super class Special : public Super { public: Special() : Super() {}; Special(const Special& _rhs) : Super(_rhs){}; virtual Special* clone() const {return(new Special(*this));}; }; // eo class Special

Note that we have implemented a clone() function that Special (and any other derivative of Super) overrides to create the correct copy.

e.g:

Super* s = new Super(); Super* s2 = s->clone(); // copy of s Special* a = new Special(); Special* b = a->clone(); // copy of a

EDIT: As other commentator pointed out, *this, not this. That'll teach me to type quickly.

EDIT2: Another correction.

EDIT3: I really should not post so quickly when in the middle of work. Modified return-type of Special::clone() for covariant return-types.

更多推荐

复制对象

本文发布于:2023-08-04 00:57:06,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1291495.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:对象

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!