C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Q: How to copy a polymorphic class?

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language (comp.lang.c++)
View previous topic :: View next topic  
Author Message
Simon Elliott
Guest





PostPosted: Tue Oct 28, 2003 9:30 pm    Post subject: Q: How to copy a polymorphic class? Reply with quote




--- 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 copy
constructed from the foo part of the bar argument I passed in.

Instead of this, I want to copy the complete bar argument and assign the
result to localFoo. Is there any way of doing this without bloggs.cpp
needing to #include "bar.h"? (I suppose I need something analogous to a
virtual copy constructor...)
--
Simon Elliott
http://www.ctsn.co.uk/






Back to top
lilburne
Guest





PostPosted: Tue Oct 28, 2003 9:34 pm    Post subject: Re: Q: How to copy a polymorphic class? Reply with quote



Simon Elliott wrote:

Quote:
(I suppose I need something analogous to a
virtual copy constructor...)

Yes:

Derived* Derived::create_new()
{
return new Derived(*this);
}


Back to top
Simon Elliott
Guest





PostPosted: Tue Oct 28, 2003 10:08 pm    Post subject: Re: Q: How to copy a polymorphic class? Reply with quote



lilburne <lilburne (AT) godzilla (DOT) net> writes
Quote:
(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/







Back to top
Simon Elliott
Guest





PostPosted: Tue Oct 28, 2003 10:10 pm    Post subject: Re: Q: How to copy a polymorphic class? Reply with quote

Rolf Magnus <ramagnus (AT) t-online (DOT) de> writes
Quote:
Derived* Derived::create_new()
{
return new Derived(*this);
}

Of course every derived class would need that function, and it would
need to be virtual or pure virtual in the base class, and it should be
const. Also, clone() is a more widely used (and more appropriate) name
for it.

I'll make it pure virtual in the base class. This will make sure no-one
can put together a derived class without this method!
--
Simon Elliott
http://www.ctsn.co.uk/







Back to top
lilburne
Guest





PostPosted: Tue Oct 28, 2003 10:49 pm    Post subject: Re: Q: How to copy a polymorphic class? Reply with quote

Simon Elliott wrote:

Quote:
lilburne <lilburne (AT) godzilla (DOT) 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.

Sorry the type returned should be that of the base class,
shouldn't type code when the cat is clawing at your leg for
food:

Base* Derived::create_new() const;


Back to top
Davlet Panech
Guest





PostPosted: Tue Oct 28, 2003 10:56 pm    Post subject: Re: Q: How to copy a polymorphic class? Reply with quote


"Rolf Magnus" <ramagnus (AT) t-online (DOT) de> wrote

Quote:
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 would
need to be virtual or pure virtual in the base class, and it should be
const. Also, clone() is a more widely used (and more appropriate) name
for it.



Also, it should return a "Base *", not "Derived *".





Back to top
Peter van Merkerk
Guest





PostPosted: Wed Oct 29, 2003 12:01 am    Post subject: Re: Q: How to copy a polymorphic class? Reply with quote

Quote:
Of course every derived class would need that function, and it would
need to be virtual or pure virtual in the base class, and it should be
const. Also, clone() is a more widely used (and more appropriate) name
for it.

Also, it should return a "Base *", not "Derived *".

No, it doesn't have to unless your compiler chokes on it. Returning "Derived
*" is legal as far as the C++ standard is concerned. See also chapter 15.6.2
of the book "The C++ Programming Language" from Bjarne Stroustrup.

--
Peter van Merkerk
peter.van.merkerk(at)dse.nl




Back to top
Peter van Merkerk
Guest





PostPosted: Wed Oct 29, 2003 12:09 am    Post subject: Re: Q: How to copy a polymorphic class? Reply with quote

Quote:
Derived* Derived::create_new()
{
return new Derived(*this);
}


Excellent. That will do the job nicely.

Sorry the type returned should be that of the base class,
shouldn't type code when the cat is clawing at your leg for
food:

Base* Derived::create_new() const;

Actually the original example you provided is correct as far as the C++
standard (section 10.3) is concerned. The only problem with it is that some
C++ compilers (including some very popular ones) do not support covariant
return types. If the compiler doesn't accept covariant return types,
returning Base* instead should work fine.
--
Peter van Merkerk
peter.van.merkerk(at)dse.nl



Back to top
lilburne
Guest





PostPosted: Wed Oct 29, 2003 12:19 am    Post subject: Re: Q: How to copy a polymorphic class? Reply with quote

Peter van Merkerk wrote:

Quote:
Derived* Derived::create_new()
{
return new Derived(*this);
}


Excellent. That will do the job nicely.

Sorry the type returned should be that of the base class,
shouldn't type code when the cat is clawing at your leg for
food:

Base* Derived::create_new() const;


Actually the original example you provided is correct as far as the C++
standard (section 10.3) is concerned. The only problem with it is that some
C++ compilers (including some very popular ones) do not support covariant
return types. If the compiler doesn't accept covariant return types,
returning Base* instead should work fine.

Unfortunately that is the case. When need to compile on
three different platforms so tend to take the line of least
resistence.


Back to top
Simon Elliott
Guest





PostPosted: Wed Oct 29, 2003 8:01 am    Post subject: Re: Q: How to copy a polymorphic class? Reply with quote

Peter van Merkerk <merkerk (AT) deadspam (DOT) com> writes
Quote:
Of course every derived class would need that function, and it would
need to be virtual or pure virtual in the base class, and it should be
const. Also, clone() is a more widely used (and more appropriate) name
for it.

Also, it should return a "Base *", not "Derived *".

No, it doesn't have to unless your compiler chokes on it. Returning "Derived
*" is legal as far as the C++ standard is concerned. See also chapter 15.6.2
of the book "The C++ Programming Language" from Bjarne Stroustrup.

FWIW, I tested the example code with Borland C++ Builder 6.0 c/w patch
4, and "derived*" worked fine.
--
Simon Elliott
http://www.ctsn.co.uk/







Back to top
Peter van Merkerk
Guest





PostPosted: Wed Oct 29, 2003 9:01 am    Post subject: Re: Q: How to copy a polymorphic class? Reply with quote

Quote:
Derived* Derived::create_new()
{
return new Derived(*this);
}

Excellent. That will do the job nicely.

Sorry the type returned should be that of the base class,
shouldn't type code when the cat is clawing at your leg for
food:

Base* Derived::create_new() const;

Actually the original example you provided is correct as far as the
C++
standard (section 10.3) is concerned. The only problem with it is
that some
C++ compilers (including some very popular ones) do not support
covariant
return types. If the compiler doesn't accept covariant return
types,
returning Base* instead should work fine.

Unfortunately that is the case. When need to compile on
three different platforms so tend to take the line of least
resistence.

Writing code that should compile on three different compilers, two of
which are extremely non-compilant, and the other reasonably compiliant
(but nowhere near being able compile something like Loki) ... I
understand what you mean.... :-(

--
Peter van Merkerk
peter.van.merkerk(at)dse.nl





Back to top
stelios xanthakis
Guest





PostPosted: Wed Oct 29, 2003 10:51 am    Post subject: Re: Q: How to copy a polymorphic class? Reply with quote

Simon Elliott <simon (AT) nospam (DOT) demon.co.uk> wrote

Quote:
Peter van Merkerk <merkerk (AT) deadspam (DOT) com> writes
No, it doesn't have to unless your compiler chokes on it. Returning "Derived
*" is legal as far as the C++ standard is concerned. See also chapter 15.6.2
of the book "The C++ Programming Language" from Bjarne Stroustrup.

FWIW, I tested the example code with Borland C++ Builder 6.0 c/w patch
4, and "derived*" worked fine.

In simple inheritance &Base==&derrived so most compilers silently
pretend to implement this. If the inheritance is virtual or base
comes from multiple inheritance, the compiler may say "covariant
return types not implemented".

I don't see much meaning in using a covariant return type in
this case. Even if you return Base or Derrived it's the same
thing (the compiler will downcast before or after returning).
Covariants are interesting in the case you know what you're
expecting, and in this case virtual does not make much sense.
My point is: covariant returns is no killer-feature.

Unless of course I missed something (?)

Stelios.
http://students.ceid.upatras.gr/~sxanth/lwc/index.html
[the regular expressions are coming]

Back to top
Rolf Magnus
Guest





PostPosted: Wed Oct 29, 2003 8:53 pm    Post subject: Re: Q: How to copy a polymorphic class? Reply with quote

lilburne wrote:

Quote:
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 would
need to be virtual or pure virtual in the base class, and it should be
const. Also, clone() is a more widely used (and more appropriate) name
for it.


Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language (comp.lang.c++) All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.