非常好。这将很好地完成工作。 - Simon Elliott http://www.ctsn.co.uk/ blockquote> --- foo.h ---class foo{public:virtual void DoStuff(void);virtual ~foo();}; --- bar.h ---#include "foo.h"class bar:public foo{public:virtual void DoStuff(void);}; --- bloggs.cpp --- #include "foo.h"extern void Bloggs(foo* myFoo){foo* localFoo = new foo(*myFoo);localFoo->DoStuff();delete localFoo;} --- main.cpp ---#include "bar.h"#include "bloggs.h"int main(int argc, char* argv[]){bar myBar;Bloggs (&myBar);return 0;} In the code above, In the function "Bloggs", localFoo is a foo copyconstructed from the foo part of the bar argument I passed in. Instead of this, I want to copy the complete bar argument and assign theresult to localFoo. Is there any way of doing this without bloggs.cppneeding to #include "bar.h"? (I suppose I need something analogous to avirtual copy constructor...)--Simon Elliott http://www.ctsn.co.uk/ 解决方案 Simon Elliott wrote: (I suppose I need something analogous to a virtual copy constructor...) Yes: Derived* Derived::create_new(){return new Derived(*this);} lilburne wrote: Simon Elliott wrote:(I suppose I need something analogous to a virtual copy constructor...) Yes: Derived* Derived::create_new() { return new Derived(*this); } Of course every derived class would need that function, and it wouldneed to be virtual or pure virtual in the base class, and it should beconst. Also, clone() is a more widely used (and more appropriate) namefor it. lilburne <li******@godzilla.net> writes(I suppose I need something analogous to a virtual copy constructor...)Yes:Derived* Derived::create_new(){ return new Derived(*this);} Excellent. That will do the job nicely.--Simon Elliott http://www.ctsn.co.uk/ 这篇关于问:如何复制多态类?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 11-03 12:47