Q: How to copy a polymorphic class?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Simon Elliott

    Q: How to copy a polymorphic class?


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







  • lilburne

    #2
    Re: Q: How to copy a polymorphic class?

    Simon Elliott wrote:
    [color=blue]
    >(I suppose I need something analogous to a
    > virtual copy constructor...)[/color]

    Yes:

    Derived* Derived::create _new()
    {
    return new Derived(*this);
    }

    Comment

    • Rolf Magnus

      #3
      Re: Q: How to copy a polymorphic class?

      lilburne wrote:
      [color=blue]
      > Simon Elliott wrote:
      >[color=green]
      >>(I suppose I need something analogous to a
      >> virtual copy constructor...)[/color]
      >
      > Yes:
      >
      > Derived* Derived::create _new()
      > {
      > return new Derived(*this);
      > }[/color]

      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.

      Comment

      • Simon Elliott

        #4
        Re: Q: How to copy a polymorphic class?

        lilburne <lilburne@godzi lla.net> writes[color=blue][color=green]
        >>(I suppose I need something analogous to a
        >> virtual copy constructor...)[/color]
        >
        >Yes:
        >
        >Derived* Derived::create _new()
        >{
        > return new Derived(*this);
        >}[/color]

        Excellent. That will do the job nicely.
        --
        Simon Elliott







        Comment

        • Simon Elliott

          #5
          Re: Q: How to copy a polymorphic class?

          Rolf Magnus <ramagnus@t-online.de> writes[color=blue][color=green]
          >> Derived* Derived::create _new()
          >> {
          >> return new Derived(*this);
          >> }[/color]
          >
          >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.[/color]

          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







          Comment

          • lilburne

            #6
            Re: Q: How to copy a polymorphic class?

            Simon Elliott wrote:
            [color=blue]
            > lilburne <lilburne@godzi lla.net> writes
            >[color=green][color=darkred]
            >>>(I suppose I need something analogous to a
            >>>virtual copy constructor...)[/color]
            >>
            >>Yes:
            >>
            >>Derived* Derived::create _new()
            >>{
            >> return new Derived(*this);
            >>}[/color]
            >
            >
            > Excellent. That will do the job nicely.[/color]

            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;

            Comment

            • Davlet Panech

              #7
              Re: Q: How to copy a polymorphic class?


              "Rolf Magnus" <ramagnus@t-online.de> wrote in message
              news:bnmoiu$gud $01$1@news.t-online.com...[color=blue]
              > lilburne wrote:
              >[color=green]
              > > Simon Elliott wrote:
              > >[color=darkred]
              > >>(I suppose I need something analogous to a
              > >> virtual copy constructor...)[/color]
              > >
              > > Yes:
              > >
              > > Derived* Derived::create _new()
              > > {
              > > return new Derived(*this);
              > > }[/color]
              >
              > 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.
              >[/color]


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




              Comment

              • Peter van Merkerk

                #8
                Re: Q: How to copy a polymorphic class?

                > > Of course every derived class would need that function, and it would[color=blue][color=green]
                > > 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.[/color]
                >
                > Also, it should return a "Base *", not "Derived *".[/color]

                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.merke rk(at)dse.nl



                Comment

                • Peter van Merkerk

                  #9
                  Re: Q: How to copy a polymorphic class?

                  > >>Derived* Derived::create _new()[color=blue][color=green][color=darkred]
                  > >>{
                  > >> return new Derived(*this);
                  > >>}[/color]
                  > >
                  > >
                  > > Excellent. That will do the job nicely.[/color]
                  >
                  > 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;[/color]

                  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.merke rk(at)dse.nl


                  Comment

                  • lilburne

                    #10
                    Re: Q: How to copy a polymorphic class?

                    Peter van Merkerk wrote:
                    [color=blue][color=green][color=darkred]
                    >>>>Derived* Derived::create _new()
                    >>>>{
                    >>>> return new Derived(*this);
                    >>>>}
                    >>>
                    >>>
                    >>>Excellent. That will do the job nicely.[/color]
                    >>
                    >>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;[/color]
                    >
                    >
                    > 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.[/color]

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

                    Comment

                    • Simon Elliott

                      #11
                      Re: Q: How to copy a polymorphic class?

                      Peter van Merkerk <merkerk@deadsp am.com> writes[color=blue][color=green][color=darkred]
                      >> > 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.[/color]
                      >>
                      >> Also, it should return a "Base *", not "Derived *".[/color]
                      >
                      >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.[/color]

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







                      Comment

                      • Peter van Merkerk

                        #12
                        Re: Q: How to copy a polymorphic class?

                        > >>>>Derived* Derived::create _new()[color=blue][color=green][color=darkred]
                        > >>>>{
                        > >>>> 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;[/color]
                        > >
                        > > Actually the original example you provided is correct as far as the[/color][/color]
                        C++[color=blue][color=green]
                        > > standard (section 10.3) is concerned. The only problem with it is[/color][/color]
                        that some[color=blue][color=green]
                        > > C++ compilers (including some very popular ones) do not support[/color][/color]
                        covariant[color=blue][color=green]
                        > > return types. If the compiler doesn't accept covariant return[/color][/color]
                        types,[color=blue][color=green]
                        > > returning Base* instead should work fine.[/color]
                        >
                        > Unfortunately that is the case. When need to compile on
                        > three different platforms so tend to take the line of least
                        > resistence.[/color]

                        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.merke rk(at)dse.nl




                        Comment

                        • stelios xanthakis

                          #13
                          Re: Q: How to copy a polymorphic class?

                          Simon Elliott <simon@nospam.d emon.co.uk> wrote in message news:<MD+GVBAcP 3n$Iw0d@courtla nds.demon.co.uk >...[color=blue]
                          > Peter van Merkerk <merkerk@deadsp am.com> writes[color=green]
                          > >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.[/color]
                          >
                          > FWIW, I tested the example code with Borland C++ Builder 6.0 c/w patch
                          > 4, and "derived*" worked fine.[/color]

                          In simple inheritance &Base==&derrive d 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.

                          [the regular expressions are coming]

                          Comment

                          Working...