Virtual CopyFrom() method

M

Marcin Kalicinski

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
 
B

Bob Hairgrove

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?

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.
 
R

Rolf Magnus

Marcin said:
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?

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
}
 
R

Rob Williscroft

Marcin Kalicinski wrote in in
comp.lang.c++:
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 *)

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

dynamic_cast is *run time* not compile time.

Rob.
 
M

Marcin Kalicinski

U¿ytkownik "Marcin Kalicinski said:
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?

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
 
J

John Harrison

Marcin Kalicinski said:
U¿ytkownik "Marcin Kalicinski" <[email protected]> napisa³ w wiadomo¶ci

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

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
 
M

Marcin Kalicinski

U¿ytkownik "John Harrison said:
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

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,768
Messages
2,569,575
Members
45,052
Latest member
KetoBeez

Latest Threads

Top