executing a child method when pointed to by a base class pointer

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Peter Oliphant

    executing a child method when pointed to by a base class pointer

    Is there a way of defining a method in a base class such that derived
    classes will call their own version, EVEN if the derived instance is
    referred to by a pointer to the base class? Note that the base class method
    is not to be abstract, and will be called if the instance was created as a
    'generic' base class instance.

    It's sort of like I want the method to be abstract to children, but concrete
    at the base level. That way I can refer to an array of pointers to base
    class elements that each will call its child's version of the method when
    asked to execute this method (without being told (i.e., cast to) it's of
    'child class' type).

    Is this possible? I could create an abstract class that has this method as
    abstract and derive both 'base class' and 'child class' from it, in which
    case a pointer to the abstract class that holds an instance of the base or
    child class will execute the approriate method. But then the 'child class'
    is no longer a child of the 'base class'. I'd rather do it like I described
    above...

    Thanks in avance for responses! : )

    [==P==]


  • David Wilkinson

    #2
    Re: executing a child method when pointed to by a base class pointer

    Peter Oliphant wrote:
    [color=blue]
    > Is there a way of defining a method in a base class such that derived
    > classes will call their own version, EVEN if the derived instance is
    > referred to by a pointer to the base class? Note that the base class method
    > is not to be abstract, and will be called if the instance was created as a
    > 'generic' base class instance.
    >
    > It's sort of like I want the method to be abstract to children, but concrete
    > at the base level. That way I can refer to an array of pointers to base
    > class elements that each will call its child's version of the method when
    > asked to execute this method (without being told (i.e., cast to) it's of
    > 'child class' type).
    >
    > Is this possible? I could create an abstract class that has this method as
    > abstract and derive both 'base class' and 'child class' from it, in which
    > case a pointer to the abstract class that holds an instance of the base or
    > child class will execute the approriate method. But then the 'child class'
    > is no longer a child of the 'base class'. I'd rather do it like I described
    > above...
    >
    > Thanks in avance for responses! : )
    >
    > [==P==]
    >[/color]
    Peter:

    Maybe I'm missing something, but why don't you just make the method
    virtual and have an implementation in the base class. Or if you don't
    like that, then make a new abstract class and do

    abstract -> base -> derived

    BTW, IMHO it is not good to use the term "child class". Use "derived
    class" or "sub-class". Child often has another meaning, like
    parent-child in Windows.

    David Wilkinson

    Comment

    • Carl Daniel [VC++ MVP]

      #3
      Re: executing a child method when pointed to by a base class pointer

      "Peter Oliphant" <poliphant@Roun dTripInc.com> wrote in message
      news:e$opo756FH A.2628@TK2MSFTN GP11.phx.gbl...[color=blue]
      > Is there a way of defining a method in a base class such that derived
      > classes will call their own version, EVEN if the derived instance is
      > referred to by a pointer to the base class? Note that the base class
      > method is not to be abstract, and will be called if the instance was
      > created as a 'generic' base class instance.
      >
      > It's sort of like I want the method to be abstract to children, but
      > concrete at the base level. That way I can refer to an array of pointers
      > to base class elements that each will call its child's version of the
      > method when asked to execute this method (without being told (i.e., cast
      > to) it's of 'child class' type).
      >
      > Is this possible? I could create an abstract class that has this method as
      > abstract and derive both 'base class' and 'child class' from it, in which
      > case a pointer to the abstract class that holds an instance of the base or
      > child class will execute the approriate method. But then the 'child class'
      > is no longer a child of the 'base class'. I'd rather do it like I
      > described above...[/color]

      This is just basic polymorphism, as implemented by virtual functions in both
      native C++ and .NET.

      Simply declare the function virtual in the base class and override it in the
      derived class(es). Calls through a pointer (or reference) to the base class
      will in fact call the implementation supplied by the derived class.

      If you don't want an implementation in the base class, declare it "pure"
      (=0) in native C++, or abstract in .NET, otherwise you'll have to supply a
      definition of the function in the base class as well.

      -cd


      Comment

      • Peter Oliphant

        #4
        Re: executing a child method when pointed to by a base class pointer

        Maybe I'M missing something, but this is what I'm experiencing:

        ref class baseClass
        {
        public:
        baseClass() {}
        ~baseClass() {}

        protected:
        virtual void func() {}
        } ;

        ref childClass : public baseClass
        {
        public:
        childClass() {}
        ~childClass() {}

        protected:
        virtual void func() override {}
        } ;

        int main()
        {
        childClass^ child = gcnew childClass() ;

        baseClass^ base_child_ptr = child ;

        base_child_ptr->func() ; // execute base version!
        }

        At least I think this is what I'm experiencing, typo I haven't noticed not
        withstanding... : )

        [==P==]

        "Carl Daniel [VC++ MVP]" <cpdaniel_remov e_this_and_nosp am@mvps.org.nos pam>
        wrote in message news:eUVGr066FH A.3804@TK2MSFTN GP14.phx.gbl...[color=blue]
        > "Peter Oliphant" <poliphant@Roun dTripInc.com> wrote in message
        > news:e$opo756FH A.2628@TK2MSFTN GP11.phx.gbl...[color=green]
        >> Is there a way of defining a method in a base class such that derived
        >> classes will call their own version, EVEN if the derived instance is
        >> referred to by a pointer to the base class? Note that the base class
        >> method is not to be abstract, and will be called if the instance was
        >> created as a 'generic' base class instance.
        >>
        >> It's sort of like I want the method to be abstract to children, but
        >> concrete at the base level. That way I can refer to an array of pointers
        >> to base class elements that each will call its child's version of the
        >> method when asked to execute this method (without being told (i.e., cast
        >> to) it's of 'child class' type).
        >>
        >> Is this possible? I could create an abstract class that has this method
        >> as abstract and derive both 'base class' and 'child class' from it, in
        >> which case a pointer to the abstract class that holds an instance of the
        >> base or child class will execute the approriate method. But then the
        >> 'child class' is no longer a child of the 'base class'. I'd rather do it
        >> like I described above...[/color]
        >
        > This is just basic polymorphism, as implemented by virtual functions in
        > both native C++ and .NET.
        >
        > Simply declare the function virtual in the base class and override it in
        > the derived class(es). Calls through a pointer (or reference) to the base
        > class will in fact call the implementation supplied by the derived class.
        >
        > If you don't want an implementation in the base class, declare it "pure"
        > (=0) in native C++, or abstract in .NET, otherwise you'll have to supply a
        > definition of the function in the base class as well.
        >
        > -cd
        >
        >[/color]


        Comment

        • Carl Daniel [VC++ MVP]

          #5
          Re: executing a child method when pointed to by a base class pointer

          "Peter Oliphant" <poliphant@Roun dTripInc.com> wrote in message
          news:eOjFxA76FH A.3388@TK2MSFTN GP11.phx.gbl...[color=blue]
          > Maybe I'M missing something, but this is what I'm experiencing:
          >[/color]
          [ code snipped ]
          [color=blue]
          >
          > At least I think this is what I'm experiencing, typo I haven't noticed not
          > withstanding... : )[/color]

          No, that definitely will execute the derived class version. Add some
          Console.WriteLi ne's (or Trace or whatever) to the two functions to see for
          sure.

          using namespace System;

          ref class baseClass
          {
          public:
          baseClass() {}
          ~baseClass() {}

          public:
          virtual void func() { Console::WriteL ine("Base version"); }
          } ;

          ref class childClass : public baseClass
          {
          public:
          childClass() {}
          ~childClass() {}

          public:
          virtual void func() override { Console::WriteL ine("Derived version"); }
          } ;

          int main()
          {
          childClass^ child = gcnew childClass() ;

          baseClass^ base_child_ptr = child ;

          base_child_ptr->func() ; // execute base version!
          }

          -cd


          Comment

          • Nishant Sivakumar

            #6
            Re: executing a child method when pointed to by a base class pointer

            Show your actual code - the code you posted won't even compile. And if it
            was fixed to get it to compile fine - by adding the class keyword before
            childClass and changing the protected access to public (for func), the
            derived class method gets invoked.

            --
            Regards,
            Nish [VC++ MVP]


            "Peter Oliphant" <poliphant@Roun dTripInc.com> wrote in message
            news:eOjFxA76FH A.3388@TK2MSFTN GP11.phx.gbl...[color=blue]
            > Maybe I'M missing something, but this is what I'm experiencing:
            >
            > ref class baseClass
            > {
            > public:
            > baseClass() {}
            > ~baseClass() {}
            >
            > protected:
            > virtual void func() {}
            > } ;
            >
            > ref childClass : public baseClass
            > {
            > public:
            > childClass() {}
            > ~childClass() {}
            >
            > protected:
            > virtual void func() override {}
            > } ;
            >
            > int main()
            > {
            > childClass^ child = gcnew childClass() ;
            >
            > baseClass^ base_child_ptr = child ;
            >
            > base_child_ptr->func() ; // execute base version!
            > }
            >
            > At least I think this is what I'm experiencing, typo I haven't noticed not
            > withstanding... : )
            >
            > [==P==]
            >
            > "Carl Daniel [VC++ MVP]" <cpdaniel_remov e_this_and_nosp am@mvps.org.nos pam>
            > wrote in message news:eUVGr066FH A.3804@TK2MSFTN GP14.phx.gbl...[color=green]
            >> "Peter Oliphant" <poliphant@Roun dTripInc.com> wrote in message
            >> news:e$opo756FH A.2628@TK2MSFTN GP11.phx.gbl...[color=darkred]
            >>> Is there a way of defining a method in a base class such that derived
            >>> classes will call their own version, EVEN if the derived instance is
            >>> referred to by a pointer to the base class? Note that the base class
            >>> method is not to be abstract, and will be called if the instance was
            >>> created as a 'generic' base class instance.
            >>>
            >>> It's sort of like I want the method to be abstract to children, but
            >>> concrete at the base level. That way I can refer to an array of pointers
            >>> to base class elements that each will call its child's version of the
            >>> method when asked to execute this method (without being told (i.e., cast
            >>> to) it's of 'child class' type).
            >>>
            >>> Is this possible? I could create an abstract class that has this method
            >>> as abstract and derive both 'base class' and 'child class' from it, in
            >>> which case a pointer to the abstract class that holds an instance of the
            >>> base or child class will execute the approriate method. But then the
            >>> 'child class' is no longer a child of the 'base class'. I'd rather do it
            >>> like I described above...[/color]
            >>
            >> This is just basic polymorphism, as implemented by virtual functions in
            >> both native C++ and .NET.
            >>
            >> Simply declare the function virtual in the base class and override it in
            >> the derived class(es). Calls through a pointer (or reference) to the
            >> base class will in fact call the implementation supplied by the derived
            >> class.
            >>
            >> If you don't want an implementation in the base class, declare it "pure"
            >> (=0) in native C++, or abstract in .NET, otherwise you'll have to supply
            >> a definition of the function in the base class as well.
            >>
            >> -cd
            >>
            >>[/color]
            >
            >[/color]


            Comment

            • Peter Oliphant

              #7
              Re: executing a child method when pointed to by a base class pointer

              The only reason it won't compile is I forgot the word 'class' in forn of the
              'childClass' definition.

              And, it turns out this code does behave correctly (i.e., execute
              'childClass' version). I must be doing something else in my 'real' code. I
              can't post all my code, you wouldn't like that, it's like 30,000 line long,
              this was a reduced version... : )

              [==P==]

              "Nishant Sivakumar" <nish@nospam.as ianetindia.com> wrote in message
              news:%236z48d76 FHA.2616@TK2MSF TNGP15.phx.gbl. ..[color=blue]
              > Show your actual code - the code you posted won't even compile. And if it
              > was fixed to get it to compile fine - by adding the class keyword before
              > childClass and changing the protected access to public (for func), the
              > derived class method gets invoked.
              >
              > --
              > Regards,
              > Nish [VC++ MVP]
              >
              >
              > "Peter Oliphant" <poliphant@Roun dTripInc.com> wrote in message
              > news:eOjFxA76FH A.3388@TK2MSFTN GP11.phx.gbl...[color=green]
              >> Maybe I'M missing something, but this is what I'm experiencing:
              >>
              >> ref class baseClass
              >> {
              >> public:
              >> baseClass() {}
              >> ~baseClass() {}
              >>
              >> protected:
              >> virtual void func() {}
              >> } ;
              >>
              >> ref childClass : public baseClass
              >> {
              >> public:
              >> childClass() {}
              >> ~childClass() {}
              >>
              >> protected:
              >> virtual void func() override {}
              >> } ;
              >>
              >> int main()
              >> {
              >> childClass^ child = gcnew childClass() ;
              >>
              >> baseClass^ base_child_ptr = child ;
              >>
              >> base_child_ptr->func() ; // execute base version!
              >> }
              >>
              >> At least I think this is what I'm experiencing, typo I haven't noticed
              >> not withstanding... : )
              >>
              >> [==P==]
              >>
              >> "Carl Daniel [VC++ MVP]"
              >> <cpdaniel_remov e_this_and_nosp am@mvps.org.nos pam> wrote in message
              >> news:eUVGr066FH A.3804@TK2MSFTN GP14.phx.gbl...[color=darkred]
              >>> "Peter Oliphant" <poliphant@Roun dTripInc.com> wrote in message
              >>> news:e$opo756FH A.2628@TK2MSFTN GP11.phx.gbl...
              >>>> Is there a way of defining a method in a base class such that derived
              >>>> classes will call their own version, EVEN if the derived instance is
              >>>> referred to by a pointer to the base class? Note that the base class
              >>>> method is not to be abstract, and will be called if the instance was
              >>>> created as a 'generic' base class instance.
              >>>>
              >>>> It's sort of like I want the method to be abstract to children, but
              >>>> concrete at the base level. That way I can refer to an array of
              >>>> pointers to base class elements that each will call its child's version
              >>>> of the method when asked to execute this method (without being told
              >>>> (i.e., cast to) it's of 'child class' type).
              >>>>
              >>>> Is this possible? I could create an abstract class that has this method
              >>>> as abstract and derive both 'base class' and 'child class' from it, in
              >>>> which case a pointer to the abstract class that holds an instance of
              >>>> the base or child class will execute the approriate method. But then
              >>>> the 'child class' is no longer a child of the 'base class'. I'd rather
              >>>> do it like I described above...
              >>>
              >>> This is just basic polymorphism, as implemented by virtual functions in
              >>> both native C++ and .NET.
              >>>
              >>> Simply declare the function virtual in the base class and override it in
              >>> the derived class(es). Calls through a pointer (or reference) to the
              >>> base class will in fact call the implementation supplied by the derived
              >>> class.
              >>>
              >>> If you don't want an implementation in the base class, declare it "pure"
              >>> (=0) in native C++, or abstract in .NET, otherwise you'll have to supply
              >>> a definition of the function in the base class as well.
              >>>
              >>> -cd
              >>>
              >>>[/color]
              >>
              >>[/color]
              >
              >[/color]


              Comment

              • Peter Oliphant

                #8
                Re: executing a child method when pointed to by a base class pointer

                Yrah, I tested my code example (which I had never tested, just thought I was
                boiling down what I actually do, apparently its not 'exactly' what I do) and
                it does execute 'childClass' version. I must be doing something different in
                my 'real code'... : )

                Sorry for the brain-dead post... LOL

                [==P==]

                "Carl Daniel [VC++ MVP]" <cpdaniel_remov e_this_and_nosp am@mvps.org.nos pam>
                wrote in message news:OIghlb76FH A.1000@tk2msftn gp13.phx.gbl...[color=blue]
                > "Peter Oliphant" <poliphant@Roun dTripInc.com> wrote in message
                > news:eOjFxA76FH A.3388@TK2MSFTN GP11.phx.gbl...[color=green]
                >> Maybe I'M missing something, but this is what I'm experiencing:
                >>[/color]
                > [ code snipped ]
                >[color=green]
                >>
                >> At least I think this is what I'm experiencing, typo I haven't noticed
                >> not withstanding... : )[/color]
                >
                > No, that definitely will execute the derived class version. Add some
                > Console.WriteLi ne's (or Trace or whatever) to the two functions to see for
                > sure.
                >
                > using namespace System;
                >
                > ref class baseClass
                > {
                > public:
                > baseClass() {}
                > ~baseClass() {}
                >
                > public:
                > virtual void func() { Console::WriteL ine("Base version"); }
                > } ;
                >
                > ref class childClass : public baseClass
                > {
                > public:
                > childClass() {}
                > ~childClass() {}
                >
                > public:
                > virtual void func() override { Console::WriteL ine("Derived version"); }
                > } ;
                >
                > int main()
                > {
                > childClass^ child = gcnew childClass() ;
                >
                > baseClass^ base_child_ptr = child ;
                >
                > base_child_ptr->func() ; // execute base version!
                > }
                >
                > -cd
                >
                >[/color]


                Comment

                • Peter Oliphant

                  #9
                  Re: executing a child method when pointed to by a base class pointer

                  Yup, it was, of course, an entirely different problem (geee, has that ever
                  happened to anyone else before?)...ROFL

                  [==P==]

                  "Carl Daniel [VC++ MVP]" <cpdaniel_remov e_this_and_nosp am@mvps.org.nos pam>
                  wrote in message news:OIghlb76FH A.1000@tk2msftn gp13.phx.gbl...[color=blue]
                  > "Peter Oliphant" <poliphant@Roun dTripInc.com> wrote in message
                  > news:eOjFxA76FH A.3388@TK2MSFTN GP11.phx.gbl...[color=green]
                  >> Maybe I'M missing something, but this is what I'm experiencing:
                  >>[/color]
                  > [ code snipped ]
                  >[color=green]
                  >>
                  >> At least I think this is what I'm experiencing, typo I haven't noticed
                  >> not withstanding... : )[/color]
                  >
                  > No, that definitely will execute the derived class version. Add some
                  > Console.WriteLi ne's (or Trace or whatever) to the two functions to see for
                  > sure.
                  >
                  > using namespace System;
                  >
                  > ref class baseClass
                  > {
                  > public:
                  > baseClass() {}
                  > ~baseClass() {}
                  >
                  > public:
                  > virtual void func() { Console::WriteL ine("Base version"); }
                  > } ;
                  >
                  > ref class childClass : public baseClass
                  > {
                  > public:
                  > childClass() {}
                  > ~childClass() {}
                  >
                  > public:
                  > virtual void func() override { Console::WriteL ine("Derived version"); }
                  > } ;
                  >
                  > int main()
                  > {
                  > childClass^ child = gcnew childClass() ;
                  >
                  > baseClass^ base_child_ptr = child ;
                  >
                  > base_child_ptr->func() ; // execute base version!
                  > }
                  >
                  > -cd
                  >
                  >[/color]


                  Comment

                  • Willy Denoyette [MVP]

                    #10
                    Re: executing a child method when pointed to by a base class pointer


                    "Carl Daniel [VC++ MVP]" <cpdaniel_remov e_this_and_nosp am@mvps.org.nos pam>
                    wrote in message news:eUVGr066FH A.3804@TK2MSFTN GP14.phx.gbl...[color=blue]
                    > "Peter Oliphant" <poliphant@Roun dTripInc.com> wrote in message
                    > news:e$opo756FH A.2628@TK2MSFTN GP11.phx.gbl...[color=green]
                    >> Is there a way of defining a method in a base class such that derived
                    >> classes will call their own version, EVEN if the derived instance is
                    >> referred to by a pointer to the base class? Note that the base class
                    >> method is not to be abstract, and will be called if the instance was
                    >> created as a 'generic' base class instance.
                    >>
                    >> It's sort of like I want the method to be abstract to children, but
                    >> concrete at the base level. That way I can refer to an array of pointers
                    >> to base class elements that each will call its child's version of the
                    >> method when asked to execute this method (without being told (i.e., cast
                    >> to) it's of 'child class' type).
                    >>
                    >> Is this possible? I could create an abstract class that has this method
                    >> as abstract and derive both 'base class' and 'child class' from it, in
                    >> which case a pointer to the abstract class that holds an instance of the
                    >> base or child class will execute the approriate method. But then the
                    >> 'child class' is no longer a child of the 'base class'. I'd rather do it
                    >> like I described above...[/color]
                    >
                    > This is just basic polymorphism, as implemented by virtual functions in
                    > both native C++ and .NET.
                    >
                    > Simply declare the function virtual in the base class and override it in
                    > the derived class(es). Calls through a pointer (or reference) to the base
                    > class will in fact call the implementation supplied by the derived class.
                    >
                    > If you don't want an implementation in the base class, declare it "pure"
                    > (=0) in native C++, or abstract in .NET, otherwise you'll have to supply a
                    > definition of the function in the base class as well.
                    >
                    > -cd
                    >
                    >[/color]

                    Not directly related to OP's problem, but a good to know is that C++ allows
                    you to call the base class implementation like in following sample, however
                    this generates unverifiable code (run Peverify on the exe image to see the
                    error message).

                    // Compile with /clr:safe

                    using namespace System;

                    ref class A
                    {
                    public:
                    virtual void f()
                    {
                    Console::WriteL ine("A::f");
                    }

                    };
                    ref class B : public A
                    {
                    public:
                    virtual void f() override
                    {
                    Console::WriteL ine("B::f");
                    }
                    };

                    int main()
                    {

                    B b;
                    b.f();
                    b.A::f(); // ------> Call the Base class function
                    return 0;
                    }

                    Willy.



                    Comment

                    • Peter Oliphant

                      #11
                      Re: executing a child method when pointed to by a base class pointer

                      Just tested that you can call base method from derived instance like you
                      described....ve ry cool! : )

                      [==P==]

                      "Willy Denoyette [MVP]" <willy.denoyett e@telenet.be> wrote in message
                      news:%23n9x2e86 FHA.3048@TK2MSF TNGP10.phx.gbl. ..[color=blue]
                      >
                      > "Carl Daniel [VC++ MVP]" <cpdaniel_remov e_this_and_nosp am@mvps.org.nos pam>
                      > wrote in message news:eUVGr066FH A.3804@TK2MSFTN GP14.phx.gbl...[color=green]
                      >> "Peter Oliphant" <poliphant@Roun dTripInc.com> wrote in message
                      >> news:e$opo756FH A.2628@TK2MSFTN GP11.phx.gbl...[color=darkred]
                      >>> Is there a way of defining a method in a base class such that derived
                      >>> classes will call their own version, EVEN if the derived instance is
                      >>> referred to by a pointer to the base class? Note that the base class
                      >>> method is not to be abstract, and will be called if the instance was
                      >>> created as a 'generic' base class instance.
                      >>>
                      >>> It's sort of like I want the method to be abstract to children, but
                      >>> concrete at the base level. That way I can refer to an array of pointers
                      >>> to base class elements that each will call its child's version of the
                      >>> method when asked to execute this method (without being told (i.e., cast
                      >>> to) it's of 'child class' type).
                      >>>
                      >>> Is this possible? I could create an abstract class that has this method
                      >>> as abstract and derive both 'base class' and 'child class' from it, in
                      >>> which case a pointer to the abstract class that holds an instance of the
                      >>> base or child class will execute the approriate method. But then the
                      >>> 'child class' is no longer a child of the 'base class'. I'd rather do it
                      >>> like I described above...[/color]
                      >>
                      >> This is just basic polymorphism, as implemented by virtual functions in
                      >> both native C++ and .NET.
                      >>
                      >> Simply declare the function virtual in the base class and override it in
                      >> the derived class(es). Calls through a pointer (or reference) to the
                      >> base class will in fact call the implementation supplied by the derived
                      >> class.
                      >>
                      >> If you don't want an implementation in the base class, declare it "pure"
                      >> (=0) in native C++, or abstract in .NET, otherwise you'll have to supply
                      >> a definition of the function in the base class as well.
                      >>
                      >> -cd
                      >>
                      >>[/color]
                      >
                      > Not directly related to OP's problem, but a good to know is that C++
                      > allows you to call the base class implementation like in following sample,
                      > however this generates unverifiable code (run Peverify on the exe image to
                      > see the error message).
                      >
                      > // Compile with /clr:safe
                      >
                      > using namespace System;
                      >
                      > ref class A
                      > {
                      > public:
                      > virtual void f()
                      > {
                      > Console::WriteL ine("A::f");
                      > }
                      >
                      > };
                      > ref class B : public A
                      > {
                      > public:
                      > virtual void f() override
                      > {
                      > Console::WriteL ine("B::f");
                      > }
                      > };
                      >
                      > int main()
                      > {
                      >
                      > B b;
                      > b.f();
                      > b.A::f(); // ------> Call the Base class function
                      > return 0;
                      > }
                      >
                      > Willy.
                      >
                      >
                      >[/color]


                      Comment

                      Working...