Connecting Tech Pros Worldwide Forums | Help | Site Map

Virtual CopyFrom() method

Marcin Kalicinski
Guest
 
Posts: n/a
#1: Jul 22 '05
Hi,

In the following code example:

class Base {
virtual void CopyFrom(const Base *);
};

class Derived: public Base {
void CopyFrom(const Derived *);
};

Derived::CopyFrom() does not override Base::CopyFrom() (am I right?)
What should I do if I want this kind of method in hierarchy to be virtual?
Do I have to change the signature of CopyFrom in Derived to

void Derived::CopyFrom(const Base *)

And do a compile time check if supplied pointer is of Derived class by using
dynamic_cast?

Best regards,
Marcin



Rolf Magnus
Guest
 
Posts: n/a
#2: Jul 22 '05

re: Virtual CopyFrom() method


Marcin Kalicinski wrote:
[color=blue]
> Hi,
>
> In the following code example:
>
> class Base {
> virtual void CopyFrom(const Base *);
> };
>
> class Derived: public Base {
> void CopyFrom(const Derived *);
> };
>
> Derived::CopyFrom() does not override Base::CopyFrom() (am I right?)
> What should I do if I want this kind of method in hierarchy to be
> virtual? Do I have to change the signature of CopyFrom in Derived to
>
> void Derived::CopyFrom(const Base *)
>
> And do a compile time check if supplied pointer is of Derived class by
> using dynamic_cast?[/color]

dynamic_cast might be unsafe in this case. If you ever derive another
class from Derived, a dynamic_cast will succeed if the argument points
to such a beast, and that might not be what you want. So you should
better use typeid in this case:

void Derived::CopyFrom(const Base* rhs)
{
if (typeid(*rhs) != typeid(Derived))
{
throw TypesDontMatch();
}

//no dynamic_cast needed anymore, since we know it's the right type
const Derived* obj = static_cast<const Derived*>(rhs);

//do the copy
}

Rob Williscroft
Guest
 
Posts: n/a
#3: Jul 22 '05

re: Virtual CopyFrom() method


Marcin Kalicinski wrote in news:cbbnp6$4pi$1@korweta.task.gda.pl in
comp.lang.c++:
[color=blue]
> Hi,
>
> In the following code example:
>
> class Base {
> virtual void CopyFrom(const Base *);
> };
>
> class Derived: public Base {
> void CopyFrom(const Derived *);
> };
>
> Derived::CopyFrom() does not override Base::CopyFrom() (am I right?)
> What should I do if I want this kind of method in hierarchy to be
> virtual? Do I have to change the signature of CopyFrom in Derived to
>
> void Derived::CopyFrom(const Base *)
>[/color]

Yes. Argument pointers or refrences should be to the same type
or to *base* of that type (the exact opposite of what you tried).
[color=blue]
> And do a compile time check if supplied pointer is of Derived class by
> using dynamic_cast?[/color]

dynamic_cast is *run time* not compile time.

Rob.
--
http://www.victim-prime.dsl.pipex.com/
Bob Hairgrove
Guest
 
Posts: n/a
#4: Jul 22 '05

re: Virtual CopyFrom() method


On Wed, 23 Jun 2004 12:58:36 +0200, "Marcin Kalicinski"
<kalita@poczta.onet.pl> wrote:
[color=blue]
>Hi,
>
>In the following code example:
>
>class Base {
> virtual void CopyFrom(const Base *);
>};
>
>class Derived: public Base {
> void CopyFrom(const Derived *);
>};
>
>Derived::CopyFrom() does not override Base::CopyFrom() (am I right?)
>What should I do if I want this kind of method in hierarchy to be virtual?
>Do I have to change the signature of CopyFrom in Derived to
>
>void Derived::CopyFrom(const Base *)
>
>And do a compile time check if supplied pointer is of Derived class by using
>dynamic_cast?[/color]

That would be one way of doing it. In fact, it's probably the only way
that makes sense. For example, A<--B and A<--C. What does it mean to
copy B from C? Or C from B? Do you want to allow this?

There was a similar thread recently in this NG called "Problem with
overriden operators" which you might find interesting.

--
Bob Hairgrove
NoSpamPlease@Home.com
Marcin Kalicinski
Guest
 
Posts: n/a
#5: Jul 22 '05

re: Virtual CopyFrom() method


Użytkownik "Marcin Kalicinski" <kalita@poczta.onet.pl> napisał w wiadomości
news:cbbnp6$4pi$1@korweta.task.gda.pl...[color=blue]
> Hi,
>
> In the following code example:
>
> class Base {
> virtual void CopyFrom(const Base *);
> };
>
> class Derived: public Base {
> void CopyFrom(const Derived *);
> };
>
> Derived::CopyFrom() does not override Base::CopyFrom() (am I right?)
> What should I do if I want this kind of method in hierarchy to be virtual?
> Do I have to change the signature of CopyFrom in Derived to
>
> void Derived::CopyFrom(const Base *)
>
> And do a compile time check if supplied pointer is of Derived class by[/color]
using[color=blue]
> dynamic_cast?[/color]

My mistake, of course dynamic_cast is a runtime factility, not compile time.
But that's why I'm asking, because I'd rather have a compile time check, if
possible.

I know that overriding virtual methods on covariant (is this the word?)
return type is allowed, so that the below should work:

virtual Base *Base::Clone() const;
virtual Derived *Derived::Clone() const;

But unfortunately CopyFrom() does not create a new object, it only copies
data to an existing one. So the semantics of Clone() is pretty much
incompatible with CopyFrom() behavior.

Anyone has any ideas on how can I have compile-time check on overridden
CopyFrom, and retain its original semantics?

Thanks,
Marcin


John Harrison
Guest
 
Posts: n/a
#6: Jul 22 '05

re: Virtual CopyFrom() method



"Marcin Kalicinski" <kalita@poczta.onet.pl> wrote in message
news:cbbuik$pgn$1@korweta.task.gda.pl...[color=blue]
> Użytkownik "Marcin Kalicinski" <kalita@poczta.onet.pl> napisał w[/color]
wiadomości[color=blue]
> news:cbbnp6$4pi$1@korweta.task.gda.pl...[color=green]
> > Hi,
> >
> > In the following code example:
> >
> > class Base {
> > virtual void CopyFrom(const Base *);
> > };
> >
> > class Derived: public Base {
> > void CopyFrom(const Derived *);
> > };
> >
> > Derived::CopyFrom() does not override Base::CopyFrom() (am I right?)
> > What should I do if I want this kind of method in hierarchy to be[/color][/color]
virtual?[color=blue][color=green]
> > Do I have to change the signature of CopyFrom in Derived to
> >
> > void Derived::CopyFrom(const Base *)
> >
> > And do a compile time check if supplied pointer is of Derived class by[/color]
> using[color=green]
> > dynamic_cast?[/color]
>
> My mistake, of course dynamic_cast is a runtime factility, not compile[/color]
time.[color=blue]
> But that's why I'm asking, because I'd rather have a compile time check,[/color]
if[color=blue]
> possible.
>
> I know that overriding virtual methods on covariant (is this the word?)
> return type is allowed, so that the below should work:
>
> virtual Base *Base::Clone() const;
> virtual Derived *Derived::Clone() const;
>
> But unfortunately CopyFrom() does not create a new object, it only copies
> data to an existing one. So the semantics of Clone() is pretty much
> incompatible with CopyFrom() behavior.
>
> Anyone has any ideas on how can I have compile-time check on overridden
> CopyFrom, and retain its original semantics?
>
> Thanks,
> Marcin
>[/color]

I don't get it. If you want compile time checks why are you using virtual
functions?

What's wrong with this?

Base b;
Derived d;
d = b; // compile time error
b = d; // ok

john


Marcin Kalicinski
Guest
 
Posts: n/a
#7: Jul 22 '05

re: Virtual CopyFrom() method



Użytkownik "John Harrison" <john_andronicus@hotmail.com> napisał w
wiadomości news:2jtda0F14rdrnU1@uni-berlin.de...[color=blue]
>
> "Marcin Kalicinski" <kalita@poczta.onet.pl> wrote in message
> news:cbbuik$pgn$1@korweta.task.gda.pl...[color=green]
> > Użytkownik "Marcin Kalicinski" <kalita@poczta.onet.pl> napisał w[/color]
> wiadomości[color=green]
> > news:cbbnp6$4pi$1@korweta.task.gda.pl...[color=darkred]
> > > Hi,
> > >
> > > In the following code example:
> > >
> > > class Base {
> > > virtual void CopyFrom(const Base *);
> > > };
> > >
> > > class Derived: public Base {
> > > void CopyFrom(const Derived *);
> > > };
> > >
> > > Derived::CopyFrom() does not override Base::CopyFrom() (am I right?)
> > > What should I do if I want this kind of method in hierarchy to be[/color][/color]
> virtual?[color=green][color=darkred]
> > > Do I have to change the signature of CopyFrom in Derived to
> > >
> > > void Derived::CopyFrom(const Base *)
> > >
> > > And do a compile time check if supplied pointer is of Derived class by[/color]
> > using[color=darkred]
> > > dynamic_cast?[/color]
> >
> > My mistake, of course dynamic_cast is a runtime factility, not compile[/color]
> time.[color=green]
> > But that's why I'm asking, because I'd rather have a compile time check,[/color]
> if[color=green]
> > possible.
> >
> > I know that overriding virtual methods on covariant (is this the word?)
> > return type is allowed, so that the below should work:
> >
> > virtual Base *Base::Clone() const;
> > virtual Derived *Derived::Clone() const;
> >
> > But unfortunately CopyFrom() does not create a new object, it only[/color][/color]
copies[color=blue][color=green]
> > data to an existing one. So the semantics of Clone() is pretty much
> > incompatible with CopyFrom() behavior.
> >
> > Anyone has any ideas on how can I have compile-time check on overridden
> > CopyFrom, and retain its original semantics?
> >
> > Thanks,
> > Marcin
> >[/color]
>
> I don't get it. If you want compile time checks why are you using virtual
> functions?
>
> What's wrong with this?
>
> Base b;
> Derived d;
> d = b; // compile time error
> b = d; // ok[/color]

I'd like to have the following:

Base b;
Derived d;
Base *bd = &d;
bd->CopyFrom(&b); // compile time error (bd is actually a Derived)
bd->CopyFrom(&d); // ok

But right now I see it's rather impossible, because compiler does not know
the real type of bd, so it cannot do any checking.

I use CopyFrom() instead of operator = because in my program Base/Derived
are large and complex types, and copy operation is quite complicated. I
don't want it to look like a simple assignment.

Best regards,
Marcin



Closed Thread