Accessing Base class function using a pointer to a derived class

Discussion in 'C++' started by Abhijit Deshpande, Jun 28, 2003.

  1. Is there any elegant way to acheive following:

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

    class Derived : public Base {
    public:
    Derived() {}
    ~Derived()
    void Method() { cout << "Derived::Method 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::Method called" and not "Derived::Method 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.
     
    Abhijit Deshpande, Jun 28, 2003
    #1
    1. Advertising

  2. Abhijit Deshpande

    Rolf Magnus Guest

    Abhijit Deshpande wrote:

    > Is there any elegant way to acheive following:
    >
    > class Base {
    > public:
    > Base() {}
    > virtual ~Base() {}
    > virtual void Method() { cout << "Base::Method called"; return;
    > }
    > };
    >
    > class Derived : public Base {
    > public:
    > Derived() {}
    > ~Derived()
    > void Method() { cout << "Derived::Method 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::Method called" and not "Derived::Method 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".


    basePtr->Base::Method();

    But please think thrice before doing that. Looks like bad design to me.
     
    Rolf Magnus, Jun 28, 2003
    #2
    1. Advertising

  3. "Abhijit Deshpande" <> wrote in message
    news:...
    > Is there any elegant way to acheive following:
    >
    > class Base {
    > public:
    > Base() {}
    > virtual ~Base() {}
    > virtual void Method() { cout << "Base::Method called"; return;
    > }
    > };
    >
    > class Derived : public Base {
    > public:
    > Derived() {}
    > ~Derived()
    > void Method() { cout << "Derived::Method 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::Method called" and not "Derived::Method 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.


    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
     
    John Harrison, Jun 28, 2003
    #3
  4. "Craig Thomson" <> wrote in message
    news:bdklpg$4r6$...
    > Hi,
    >
    > > 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".

    >
    > I'm a little hazy on the whole casting business, but as I understand it

    what
    > you want is:
    >
    > main() {
    > Derived deriveObj;
    > Base * basePtr = 0;
    >
    > basePtr = dynamic_cast<Base*>(&DeriveObj);
    >
    > basePtr->Method();
    > }
    >
    > or
    >
    > main() {
    > Derived deriveObj;
    > Base * basePtr = 0;
    >
    > basePtr = static_cast<Base*>(&DeriveObj);
    >
    > basePtr->Method();
    > }
    >
    > Dynamic cast is safer because it does run time type checking but as long

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


    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
     
    John Harrison, Jun 28, 2003
    #4
  5. 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_cast<DummyBase
    *>(&deriveObj);

    dummyBasePtr->Method();

    return 0;
    }

    This should print "Base::Method 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::Method
    called".

    Regards,
    Abhijit.

    "John Harrison" <> wrote in message news:<bdkjor$u82sv$>...
    > "Craig Thomson" <> wrote in message
    > news:bdklpg$4r6$...
    > > Hi,
    > >
    > > > 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".

    > >
    > > I'm a little hazy on the whole casting business, but as I understand it

    > what
    > > you want is:
    > >
    > > main() {
    > > Derived deriveObj;
    > > Base * basePtr = 0;
    > >
    > > basePtr = dynamic_cast<Base*>(&DeriveObj);
    > >
    > > basePtr->Method();
    > > }
    > >
    > > or
    > >
    > > main() {
    > > Derived deriveObj;
    > > Base * basePtr = 0;
    > >
    > > basePtr = static_cast<Base*>(&DeriveObj);
    > >
    > > basePtr->Method();
    > > }
    > >
    > > Dynamic cast is safer because it does run time type checking but as long

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

    >
    > 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
     
    Abhijit Deshpande, Jun 30, 2003
    #5
  6. Abhijit Deshpande wrote:
    >
    > 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_cast<DummyBase
    > *>(&deriveObj);
    >


    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.


    > dummyBasePtr->Method();
    >
    > return 0;
    > }
    >
    > This should print "Base::Method called". Is there anything
    > conceptually wrong in above piece of code??


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


    --
    Karl Heinz Buchegger
     
    Karl Heinz Buchegger, Jun 30, 2003
    #6
  7. Well, here is the complete piece of code, I tried..

    class Base {

    public:

    Base() {
    };

    virtual ~Base() {
    };

    virtual void Method() {
    printf("Base::Method 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_cast<DummyBase *>(&derivedObj);

    dummyBasePtr->Method();
    }

    And the expected output is "Base::Method called.", whereas the actual
    output is "Derived::Method called."

    Regards,
    Abhijit.

    Karl Heinz Buchegger <> wrote in message news:<>...
    > Abhijit Deshpande wrote:
    > >
    > > 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_cast<DummyBase
    > > *>(&deriveObj);
    > >

    >
    > 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.
    >
    >
    > > dummyBasePtr->Method();
    > >
    > > return 0;
    > > }
    > >
    > > This should print "Base::Method called". Is there anything
    > > conceptually wrong in above piece of code??

    >
    > Impossible to say without seeing the actual, complete code you used to test it.
     
    Abhijit Deshpande, Jul 1, 2003
    #7
  8. Abhijit Deshpande

    samualmenon

    Joined:
    Jul 16, 2008
    Messages:
    1
    If your "Method" is virtual its going to call the derived method

    Since your Base class "Method" is virtual its going to call the derived method and expected output will be Derived method and not the base Method. Try removing virtual from Base Method and you should see what you want to see.:captain:
     
    samualmenon, Jul 16, 2008
    #8
    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. tirath
    Replies:
    3
    Views:
    729
    Ivan Vecerina
    Oct 12, 2003
  2. Banaticus  Bart
    Replies:
    9
    Views:
    630
    Gary Labowitz
    May 7, 2004
  3. Replies:
    1
    Views:
    412
    myork
    May 23, 2007
  4. Replies:
    1
    Views:
    400
    Victor Bazarov
    May 23, 2007
  5. , India
    Replies:
    8
    Views:
    1,000
    gwowen
    Aug 18, 2010
Loading...

Share This Page