Pointers to base class (invoking methods of child classes)

Discussion in 'C++' started by Alfonso Morra, Sep 1, 2005.

  1. Hi,

    I have a variable that stores a pointer to a base class. I am using this
    variable to store pointers to objects of the base class, as well as
    pointers to other derived classes.

    However, the derived classes have methods (not available on the base
    class) that I would like to invoke. I thought I could simply cast the
    pointer to the appropriate derived class and access the methods this way
    - but that dosen't work.

    Example :

    class A {
    public:
    A() ;
    ~A() ;
    void foo(void) ;
    };

    class B: public A {
    public:
    B() ;
    ~B() ;
    int bar(char*) ;
    };


    class C : public A {
    public:
    C();
    ~C();

    double foobar(int, int, double ) ;
    };

    //variable to hold ptr to base class:

    A *base_ptr = new C; // ptr to A or B or C can be stored in variable

    How can I invoke foobar() on base_ptr ?


    tkx
    Alfonso Morra, Sep 1, 2005
    #1
    1. Advertising

  2. Alfonso Morra wrote:
    > I have a variable that stores a pointer to a base class. I am using
    > this variable to store pointers to objects of the base class, as well
    > as pointers to other derived classes.
    >
    > However, the derived classes have methods (not available on the base
    > class) that I would like to invoke. I thought I could simply cast the
    > pointer to the appropriate derived class and access the methods this
    > way - but that dosen't work.
    >
    > Example :
    >
    > class A {
    > public:
    > A() ;
    > ~A() ;
    > void foo(void) ;
    > };
    >
    > class B: public A {
    > public:
    > B() ;
    > ~B() ;
    > int bar(char*) ;
    > };
    >
    >
    > class C : public A {
    > public:
    > C();
    > ~C();
    >
    > double foobar(int, int, double ) ;
    > };
    >
    > //variable to hold ptr to base class:
    >
    > A *base_ptr = new C; // ptr to A or B or C can be stored in variable


    Since you allocate using 'new' and keep the base class pointer, you
    will need to delete it at some point. Your base class _destructor_
    MUST be virtual to avoid undefined behaviour.

    > How can I invoke foobar() on base_ptr ?


    dynamic_cast<C*>(base_ptr)->foobar(...

    or

    static_cast<C*>(base_ptr)->foobar(...

    since you know that the original class _was_ 'C'.

    V
    Victor Bazarov, Sep 1, 2005
    #2
    1. Advertising

  3. Alfonso Morra

    Old Wolf Guest

    Alfonso Morra wrote:
    > Hi,
    >
    > I have a variable that stores a pointer to a base class. I am using
    > this variable to store pointers to objects of the base class, as well
    > as pointers to other derived classes.
    >
    > However, the derived classes have methods (not available on the
    > base class) that I would like to invoke. I thought I could simply
    > cast the pointer to the appropriate derived class and access the
    > methods this way - but that dosen't work.


    Explain what you mean by "dosen't work"

    >
    > Example :
    >
    > class A {
    > public:
    > A() ;
    > ~A() ;
    > void foo(void) ;
    > };
    >
    > class B: public A {
    > public:
    > B() ;
    > ~B() ;
    > int bar(char*) ;
    > };
    >
    >
    > class C : public A {
    > public:
    > C();
    > ~C();
    >
    > double foobar(int, int, double ) ;
    > };
    >
    > //variable to hold ptr to base class:
    >
    > A *base_ptr = new C; // ptr to A or B or C can be stored in variable
    >
    > How can I invoke foobar() on base_ptr ?


    dynamic_cast<C &>(*base_ptr).foobar()

    This will throw an exception if base_ptr doesn't point to a C.
    Alternatively:

    C *ptr = dynamic_cast<C *>(base_ptr);
    if (ptr)
    ptr->foobar();
    else
    cout << "base_ptr did not point to a C";
    Old Wolf, Sep 1, 2005
    #3
  4. Alfonso Morra wrote:

    > Hi,
    >
    > I have a variable that stores a pointer to a base class. I am using this
    > variable to store pointers to objects of the base class, as well as
    > pointers to other derived classes.
    >
    > However, the derived classes have methods (not available on the base
    > class) that I would like to invoke. I thought I could simply cast the
    > pointer to the appropriate derived class and access the methods this way
    > - but that dosen't work.
    >
    > Example :
    >
    > class A {
    > public:
    > A() ;
    > ~A() ;
    > void foo(void) ;
    > };
    >
    > class B: public A {
    > public:
    > B() ;
    > ~B() ;
    > int bar(char*) ;
    > };
    >
    >
    > class C : public A {
    > public:
    > C();
    > ~C();
    >
    > double foobar(int, int, double ) ;
    > };
    >
    > //variable to hold ptr to base class:
    >
    > A *base_ptr = new C; // ptr to A or B or C can be stored in variable
    >
    > How can I invoke foobar() on base_ptr ?
    >
    >
    > tkx
    >

    Thanks guys
    Alfonso Morra, Sep 1, 2005
    #4
  5. Old Wolf wrote:
    > Alternatively:
    >
    > C *ptr = dynamic_cast<C *>(base_ptr);
    > if (ptr)
    > ptr->foobar();
    > else
    > cout << "base_ptr did not point to a C";
    >


    Alternatively

    if (C *ptr = dynamic_cast<C*>(base_ptr))
    ptr->foobar();
    else
    cout << "bleh";

    (avoids placing 'ptr' into the surrounding scope)

    V
    Victor Bazarov, Sep 1, 2005
    #5
    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. Vikas
    Replies:
    1
    Views:
    340
    Victor Bazarov
    Aug 25, 2003
  2. mrstephengross
    Replies:
    5
    Views:
    596
    Larry I Smith
    May 18, 2005
  3. Jeff Rodriguez
    Replies:
    23
    Views:
    1,119
    David Schwartz
    Dec 9, 2003
  4. bgold
    Replies:
    12
    Views:
    1,017
    James Kanze
    Jan 1, 2008
  5. Kenneth McDonald
    Replies:
    5
    Views:
    315
    Kenneth McDonald
    Sep 26, 2008
Loading...

Share This Page