Accessing Base class function using a pointer to a derived class

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Abhijit Deshpande

    Accessing Base class function using a pointer to a derived class

    Is there any elegant way to acheive following:

    class Base {
    public:
    Base() {}
    virtual ~Base() {}
    virtual void Method() { cout << "Base::Meth od called"; return;
    }
    };

    class Derived : public Base {
    public:
    Derived() {}
    ~Derived()
    void Method() { cout << "Derived::Metho d called"; return; }
    };

    int main() {
    Derived deriveObj;
    Base * basePtr = 0;

    basePtr = <some kind of cast????> &deriveObj;

    basePtr->Method();
    }

    In the above code, the call "basePtr->Method" should print
    "Base::Meth od called" and not "Derived::Metho d called".

    Is there any way to assign address of "deriveObj" to "basePtr" so that
    the virtual mechanism is bypassed and call to member function "Method"
    actually calls the member function from the "class Base" and not from
    "class Derived".

    Thanks in advance,
    Regards,
    Abhijit.
  • John Harrison

    #2
    Re: Accessing Base class function using a pointer to a derived class


    "Abhijit Deshpande" <abhijit.deshpa nde@lycos.com> wrote in message
    news:8d1b01ba.0 306280456.12ff7 ba1@posting.goo gle.com...[color=blue]
    > Is there any elegant way to acheive following:
    >
    > class Base {
    > public:
    > Base() {}
    > virtual ~Base() {}
    > virtual void Method() { cout << "Base::Meth od called"; return;
    > }
    > };
    >
    > class Derived : public Base {
    > public:
    > Derived() {}
    > ~Derived()
    > void Method() { cout << "Derived::Metho d called"; return; }
    > };
    >
    > int main() {
    > Derived deriveObj;
    > Base * basePtr = 0;
    >
    > basePtr = <some kind of cast????> &deriveObj;
    >
    > basePtr->Method();
    > }
    >
    > In the above code, the call "basePtr->Method" should print
    > "Base::Meth od called" and not "Derived::Metho d called".
    >
    > Is there any way to assign address of "deriveObj" to "basePtr" so that
    > the virtual mechanism is bypassed and call to member function "Method"
    > actually calls the member function from the "class Base" and not from
    > "class Derived".
    >
    > Thanks in advance,
    > Regards,
    > Abhijit.[/color]

    You can do this

    Derived deriveObj;
    Base * basePtr = 0;

    basePtr = &deriveObj;

    basePtr->Base::Method() ; // calls Base::Method

    but there is no way to bypass the virtual machanism when you assign a
    pointer.

    john


    Comment

    • Rolf Magnus

      #3
      Re: Accessing Base class function using a pointer to a derived class

      Abhijit Deshpande wrote:
      [color=blue]
      > Is there any elegant way to acheive following:
      >
      > class Base {
      > public:
      > Base() {}
      > virtual ~Base() {}
      > virtual void Method() { cout << "Base::Meth od called"; return;
      > }
      > };
      >
      > class Derived : public Base {
      > public:
      > Derived() {}
      > ~Derived()
      > void Method() { cout << "Derived::Metho d called"; return; }
      > };
      >
      > int main() {
      > Derived deriveObj;
      > Base * basePtr = 0;
      >
      > basePtr = <some kind of cast????> &deriveObj;
      >
      > basePtr->Method();
      > }
      >
      > In the above code, the call "basePtr->Method" should print
      > "Base::Meth od called" and not "Derived::Metho d called".
      >
      > Is there any way to assign address of "deriveObj" to "basePtr" so that
      > the virtual mechanism is bypassed and call to member function "Method"
      > actually calls the member function from the "class Base" and not from
      > "class Derived".[/color]

      basePtr->Base::Method() ;

      But please think thrice before doing that. Looks like bad design to me.

      Comment

      • John Harrison

        #4
        Re: Accessing Base class function using a pointer to a derived class


        "Craig Thomson" <craig@craigtho mson.me.uk> wrote in message
        news:bdklpg$4r6 $1@news.ukfsn.o rg...[color=blue]
        > Hi,
        >[color=green]
        > > Is there any way to assign address of "deriveObj" to "basePtr" so that
        > > the virtual mechanism is bypassed and call to member function "Method"
        > > actually calls the member function from the "class Base" and not from
        > > "class Derived".[/color]
        >
        > I'm a little hazy on the whole casting business, but as I understand it[/color]
        what[color=blue]
        > you want is:
        >
        > main() {
        > Derived deriveObj;
        > Base * basePtr = 0;
        >
        > basePtr = dynamic_cast<Ba se*>(&DeriveObj );
        >
        > basePtr->Method();
        > }
        >
        > or
        >
        > main() {
        > Derived deriveObj;
        > Base * basePtr = 0;
        >
        > basePtr = static_cast<Bas e*>(&DeriveObj) ;
        >
        > basePtr->Method();
        > }
        >
        > Dynamic cast is safer because it does run time type checking but as long[/color]
        as[color=blue]
        > your going from derived to base class static cast should work fine too.
        > Google for dynamic_cast or static_cast and you should get all the
        > information you need.
        >[/color]

        Neither type of cast is necessary when converting from a derived class
        pointer to a base class pointer, and neither cast achieves what the OP wants
        which is to override the virtual function calling mechanism.

        You've got this mixed up with converting from a base class pointer to a
        derived class pointer, when some sort of cast is necessary.

        john


        Comment

        • Abhijit Deshpande

          #5
          Re: Accessing Base class function using a pointer to a derived class

          Thanks John and Ralf for the solution.

          But I was wondering, should following piece of code work?
          In addition to "class Base" and "class Derived", we define one more
          class,

          class DummyBase() {

          public:

          DummyBase() {
          }

          ~DummyBase() {
          }

          void Method() {
          Base::Method();

          return;
          }
          };

          int main() {
          Derived deriveObj;
          DummyBase * dummyBasePtr = reinterpret_cas t<DummyBase
          *>(&deriveObj) ;

          dummyBasePtr->Method();

          return 0;
          }

          This should print "Base::Meth od called". Is there anything
          conceptually wrong in above piece of code?? Because, I tried it using
          GNU g++ on RedHat linux 7.2, and it still prints "Derived::Metho d
          called".

          Regards,
          Abhijit.

          "John Harrison" <john_andronicu s@hotmail.com> wrote in message news:<bdkjor$u8 2sv$1@ID-196037.news.dfn cis.de>...[color=blue]
          > "Craig Thomson" <craig@craigtho mson.me.uk> wrote in message
          > news:bdklpg$4r6 $1@news.ukfsn.o rg...[color=green]
          > > Hi,
          > >[color=darkred]
          > > > Is there any way to assign address of "deriveObj" to "basePtr" so that
          > > > the virtual mechanism is bypassed and call to member function "Method"
          > > > actually calls the member function from the "class Base" and not from
          > > > "class Derived".[/color]
          > >
          > > I'm a little hazy on the whole casting business, but as I understand it[/color]
          > what[color=green]
          > > you want is:
          > >
          > > main() {
          > > Derived deriveObj;
          > > Base * basePtr = 0;
          > >
          > > basePtr = dynamic_cast<Ba se*>(&DeriveObj );
          > >
          > > basePtr->Method();
          > > }
          > >
          > > or
          > >
          > > main() {
          > > Derived deriveObj;
          > > Base * basePtr = 0;
          > >
          > > basePtr = static_cast<Bas e*>(&DeriveObj) ;
          > >
          > > basePtr->Method();
          > > }
          > >
          > > Dynamic cast is safer because it does run time type checking but as long[/color]
          > as[color=green]
          > > your going from derived to base class static cast should work fine too.
          > > Google for dynamic_cast or static_cast and you should get all the
          > > information you need.
          > >[/color]
          >
          > Neither type of cast is necessary when converting from a derived class
          > pointer to a base class pointer, and neither cast achieves what the OP wants
          > which is to override the virtual function calling mechanism.
          >
          > You've got this mixed up with converting from a base class pointer to a
          > derived class pointer, when some sort of cast is necessary.
          >
          > john[/color]

          Comment

          • Karl Heinz Buchegger

            #6
            Re: Accessing Base class function using a pointer to a derived class



            Abhijit Deshpande wrote:[color=blue]
            >
            > Thanks John and Ralf for the solution.
            >
            > But I was wondering, should following piece of code work?
            > In addition to "class Base" and "class Derived", we define one more
            > class,
            >
            > class DummyBase() {[/color]
            [color=blue]
            >
            > public:
            >
            > DummyBase() {
            > }
            >
            > ~DummyBase() {
            > }
            >
            > void Method() {
            > Base::Method();
            >
            > return;
            > }
            > };
            >
            > int main() {
            > Derived deriveObj;
            > DummyBase * dummyBasePtr = reinterpret_cas t<DummyBase
            > *>(&deriveObj) ;
            >[/color]

            You are casting way to much!
            What (if any) is the relationship of Derived and DummyBase.
            Please show it with code and not with english descriptions.

            [color=blue]
            > dummyBasePtr->Method();
            >
            > return 0;
            > }
            >
            > This should print "Base::Meth od called". Is there anything
            > conceptually wrong in above piece of code??[/color]

            Impossible to say without seeing the actual, complete code you used to test it.


            --
            Karl Heinz Buchegger
            kbuchegg@gascad .at

            Comment

            • Abhijit Deshpande

              #7
              Re: Accessing Base class function using a pointer to a derived class

              Well, here is the complete piece of code, I tried..

              class Base {

              public:

              Base() {
              };

              virtual ~Base() {
              };

              virtual void Method() {
              printf("Base::M ethod called.\n");

              return;
              }
              };

              class Derived : public Base {

              public:

              Derived() {
              };

              ~Derived() {
              };

              void Method() {
              printf("Derived ::Method called.\n");

              return;
              }
              };

              class DummyBase : public Base {

              public:

              DummyBase() {
              }

              ~DummyBase() {
              }

              void Method() {
              Base::Method();

              return;
              }
              };

              int main() {

              Derived derivedObj;
              DummyBase *dummyBasePtr;

              dummyBasePtr = reinterpret_cas t<DummyBase *>(&derivedObj) ;

              dummyBasePtr->Method();
              }

              And the expected output is "Base::Meth od called.", whereas the actual
              output is "Derived::Metho d called."

              Regards,
              Abhijit.

              Karl Heinz Buchegger <kbuchegg@gasca d.at> wrote in message news:<3F0025CC. DD54EAA2@gascad .at>...[color=blue]
              > Abhijit Deshpande wrote:[color=green]
              > >
              > > Thanks John and Ralf for the solution.
              > >
              > > But I was wondering, should following piece of code work?
              > > In addition to "class Base" and "class Derived", we define one more
              > > class,
              > >
              > > class DummyBase() {[/color]
              >[color=green]
              > >
              > > public:
              > >
              > > DummyBase() {
              > > }
              > >
              > > ~DummyBase() {
              > > }
              > >
              > > void Method() {
              > > Base::Method();
              > >
              > > return;
              > > }
              > > };
              > >
              > > int main() {
              > > Derived deriveObj;
              > > DummyBase * dummyBasePtr = reinterpret_cas t<DummyBase
              > > *>(&deriveObj) ;
              > >[/color]
              >
              > You are casting way to much!
              > What (if any) is the relationship of Derived and DummyBase.
              > Please show it with code and not with english descriptions.
              >
              >[color=green]
              > > dummyBasePtr->Method();
              > >
              > > return 0;
              > > }
              > >
              > > This should print "Base::Meth od called". Is there anything
              > > conceptually wrong in above piece of code??[/color]
              >
              > Impossible to say without seeing the actual, complete code you used to test it.[/color]

              Comment

              Working...