Virtual CopyFrom() method

Discussion in 'C++' started by Marcin Kalicinski, Jun 23, 2004.

  1. 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
     
    Marcin Kalicinski, Jun 23, 2004
    #1
    1. Advertising

  2. On Wed, 23 Jun 2004 12:58:36 +0200, "Marcin Kalicinski"
    <> wrote:

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

    --
    Bob Hairgrove
     
    Bob Hairgrove, Jun 23, 2004
    #2
    1. Advertising

  3. Marcin Kalicinski

    Rolf Magnus Guest

    Marcin Kalicinski wrote:

    > 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
    }
     
    Rolf Magnus, Jun 23, 2004
    #3
  4. Marcin Kalicinski wrote in news:cbbnp6$4pi$ 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.
    --
    http://www.victim-prime.dsl.pipex.com/
     
    Rob Williscroft, Jun 23, 2004
    #4
  5. U¿ytkownik "Marcin Kalicinski" <> napisa³ w wiadomo¶ci
    news:cbbnp6$4pi$...
    > 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
     
    Marcin Kalicinski, Jun 23, 2004
    #5
  6. "Marcin Kalicinski" <> wrote in message
    news:cbbuik$pgn$...
    > U¿ytkownik "Marcin Kalicinski" <> napisa³ w

    wiadomo¶ci
    > news:cbbnp6$4pi$...
    > > 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
    >


    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
     
    John Harrison, Jun 23, 2004
    #6
  7. U¿ytkownik "John Harrison" <> napisa³ w
    wiadomo¶ci news:...
    >
    > "Marcin Kalicinski" <> wrote in message
    > news:cbbuik$pgn$...
    > > U¿ytkownik "Marcin Kalicinski" <> napisa³ w

    > wiadomo¶ci
    > > news:cbbnp6$4pi$...
    > > > 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
    > >

    >
    > 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
     
    Marcin Kalicinski, Jun 23, 2004
    #7
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Sky Sigal
    Replies:
    0
    Views:
    386
    Sky Sigal
    Jul 31, 2004
  2. Calvin Lai
    Replies:
    7
    Views:
    556
    Calvin Lai
    Dec 18, 2003
  3. jlopes
    Replies:
    5
    Views:
    453
    le ténébreux
    Nov 19, 2004
  4. jlopes
    Replies:
    7
    Views:
    427
    jlopes
    Nov 19, 2004
  5. a
    Replies:
    7
    Views:
    364
    dasjotre
    Jun 28, 2007
Loading...

Share This Page