Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Calling a method with the pointer to a base class

Calling a method with the pointer to a base class

Scheduled Pinned Locked Moved C / C++ / MFC
6 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • F Offline
    F Offline
    FriendOfAsherah
    wrote on last edited by
    #1

    I´m sure there is a simple solution but it drives me crazy:

    class A
    { virtual bool method M() };

    class B : public A { .... }

    class C1 : public B
    { virtual bool method M() override };
    class C2 : public B
    { virtual bool method M() override };

    class D
    {
    static bool DoSomething( A* ptr ) { ptr -> M(); }
    }

    Now I am working in a codepiece of C1 which calls D::DoDomething( )

    C1::anymethod()
    {
    D::DoSomething( this );
    }

    Ok, thats simple: C1::M() will be called because of the inheritance But now I wanna have that D calls the M() of C1´s base A by typecasting the this ptr to a base:

    C1::anymethod()
    {
    D::DoSomething( (A*)this );
    }

    but this will also call C1:M(). How can I tell the D::DoSomething function "If you use M1() then you have to call A::M1() " with knowing that in DoSomething by the type of the ptr?

    J 1 Reply Last reply
    0
    • F FriendOfAsherah

      I´m sure there is a simple solution but it drives me crazy:

      class A
      { virtual bool method M() };

      class B : public A { .... }

      class C1 : public B
      { virtual bool method M() override };
      class C2 : public B
      { virtual bool method M() override };

      class D
      {
      static bool DoSomething( A* ptr ) { ptr -> M(); }
      }

      Now I am working in a codepiece of C1 which calls D::DoDomething( )

      C1::anymethod()
      {
      D::DoSomething( this );
      }

      Ok, thats simple: C1::M() will be called because of the inheritance But now I wanna have that D calls the M() of C1´s base A by typecasting the this ptr to a base:

      C1::anymethod()
      {
      D::DoSomething( (A*)this );
      }

      but this will also call C1:M(). How can I tell the D::DoSomething function "If you use M1() then you have to call A::M1() " with knowing that in DoSomething by the type of the ptr?

      J Offline
      J Offline
      Jochen Arndt
      wrote on last edited by
      #2

      The simplest solution would be to create another non-virtual function in class A:

      class A
      {
      bool method AM();
      virtual bool method M() { return AM(); }
      };

      class D
      {
      static bool DoSomething( A* ptr ) { ptr ->AM(); }
      };

      F 1 Reply Last reply
      0
      • J Jochen Arndt

        The simplest solution would be to create another non-virtual function in class A:

        class A
        {
        bool method AM();
        virtual bool method M() { return AM(); }
        };

        class D
        {
        static bool DoSomething( A* ptr ) { ptr ->AM(); }
        };

        F Offline
        F Offline
        FriendOfAsherah
        wrote on last edited by
        #3

        oh , the example I have is more tricky: D should call "the next M() of any parent above the child class C1" Its vice versa to the virtual inheritance when you call the last one in the hierarchy ... if I call

        C1::anytest ()
        {
        B::M();
        }

        it goes "up" the hierarchy an calls the next found M(); in this case A::M() But in class D´s DoSomething where I get a ptr to C1, or C2 ... how can do it there?

        J _ L 3 Replies Last reply
        0
        • F FriendOfAsherah

          oh , the example I have is more tricky: D should call "the next M() of any parent above the child class C1" Its vice versa to the virtual inheritance when you call the last one in the hierarchy ... if I call

          C1::anytest ()
          {
          B::M();
          }

          it goes "up" the hierarchy an calls the next found M(); in this case A::M() But in class D´s DoSomething where I get a ptr to C1, or C2 ... how can do it there?

          J Offline
          J Offline
          Jochen Arndt
          wrote on last edited by
          #4

          Sorry, this was not clear from your initial question:

          Quote:

          But now I wanna have that D calls the M() of C1´s base A

          But you can do the same: Implement a non-virtual function B::BM() which is called by the virtual function B::M() and so on. Then you can call the non-virtual functions AM(), BM(), and so on or the virtual M() function. If you want to call the M() function of the next upper class in the hierarchy, you need to know the real class of the passed argument during run-time. This requires that your classes support some kind of Run-time type information - Wikipedia, the free encyclopedia[^].

          1 Reply Last reply
          0
          • F FriendOfAsherah

            oh , the example I have is more tricky: D should call "the next M() of any parent above the child class C1" Its vice versa to the virtual inheritance when you call the last one in the hierarchy ... if I call

            C1::anytest ()
            {
            B::M();
            }

            it goes "up" the hierarchy an calls the next found M(); in this case A::M() But in class D´s DoSomething where I get a ptr to C1, or C2 ... how can do it there?

            _ Offline
            _ Offline
            _Flaviu
            wrote on last edited by
            #5

            I think that here[^] you will find a solution, in fact, the same solution like Jochen said.

            1 Reply Last reply
            0
            • F FriendOfAsherah

              oh , the example I have is more tricky: D should call "the next M() of any parent above the child class C1" Its vice versa to the virtual inheritance when you call the last one in the hierarchy ... if I call

              C1::anytest ()
              {
              B::M();
              }

              it goes "up" the hierarchy an calls the next found M(); in this case A::M() But in class D´s DoSomething where I get a ptr to C1, or C2 ... how can do it there?

              L Offline
              L Offline
              leon de boer
              wrote on last edited by
              #6

              Ah the joys of deep class hierarchy in C++ and OOP coding circa 1980-1990 :-) Probably too much danger in doing what should be done with that old code and using modern Composition over inheritance style. It isn't going to be fun and C++ is going to fight you all the way as it will try to discourage that style. Working with old code I feel your pain ... good luck.

              In vino veritas

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups