Call derived class function from base class reference

Discussion in 'C++' started by Javier, Jun 8, 2007.

  1. Javier

    Javier Guest

    Hello, is this possible?
    I have a pure virtual function in the base class (to force the
    programmers of the derived classes to have this function implemented)
    but I want to call the derived class function from a base class
    reference:

    class A
    {
    public:
    virtual void function() = 0;
    };

    class B
    :
    public A
    {
    public:
    void function() { cout << "Hello" << endl; };
    }

    int main()
    {
    B derived();
    A &base = dynamic_cast<A &>(B); // don't now if this is the way

    base.function(); // I need this type of call because I dont't
    know the derived class type at runtime.
    }


    Of course I could have a function2 (private) and make it pure virtual,
    and then have function() in base class calling the virtual function2:

    class A
    {
    public:
    void function() { this->function_impl() };

    protected:
    virtual void function_impl() = 0;
    };

    class B
    :
    public A
    {
    protected:
    void function_impl() { cout << "Hello" << endl; };
    }

    int main()
    {
    B derived();
    A &base = dynamic_cast<A &>(B);

    A.function();
    }

    Is this good?
    But... can I do it the first way?

    Thanks.
    Javier, Jun 8, 2007
    #1
    1. Advertising

  2. Javier wrote:
    > Hello, is this possible?
    > I have a pure virtual function in the base class (to force the
    > programmers of the derived classes to have this function implemented)
    > but I want to call the derived class function from a base class
    > reference:
    >
    > class A
    > {
    > public:
    > virtual void function() = 0;
    > };
    >
    > class B
    > :
    > public A
    > {
    > public:
    > void function() { cout << "Hello" << endl; };
    > }

    ;

    You've missed a semicolon.

    >
    > int main()
    > {
    > B derived();


    'derived' here is a function. Drop the parentheses.

    > A &base = dynamic_cast<A &>(B); // don't now if this is the way


    Of course this is not correct. First of all, if you have dropped
    the parentheses, you _could_ do

    A &base = dynamic_cast<A&>(derived);

    but you don't need to, the conversion is _implicit_:

    A &base = derived;

    >
    > base.function(); // I need this type of call because I dont't
    > know the derived class type at runtime.


    That should work OK, after you fix the declarations.

    > }
    >
    >
    > Of course I could have a function2 (private) and make it pure virtual,
    > and then have function() in base class calling the virtual function2:
    >
    > class A
    > {
    > public:
    > void function() { this->function_impl() };
    >
    > protected:
    > virtual void function_impl() = 0;
    > };
    >
    > class B
    > :
    > public A
    > {
    > protected:
    > void function_impl() { cout << "Hello" << endl; };
    > }

    ;

    >
    > int main()
    > {
    > B derived();
    > A &base = dynamic_cast<A &>(B);
    >
    > A.function();
    > }
    >
    > Is this good?


    That's just as OK, given that you fix the declarations and get
    rid of the dynamic_cast.

    > But... can I do it the first way?


    Should be OK.

    V
    --
    Please remove capital 'A's when replying by e-mail
    I do not respond to top-posted replies, please don't ask
    Victor Bazarov, Jun 8, 2007
    #2
    1. Advertising

  3. Daniel Kraft wrote:
    > Javier wrote:
    >> class A
    >> {
    >> public:
    >> virtual void function() = 0;
    >> };
    >>
    >> class B
    >>>

    >> public A
    >> {
    >> public:
    >> void function() { cout << "Hello" << endl; };
    >> }
    >>
    >> int main()
    >> {
    >> B derived();
    >> A &base = dynamic_cast<A &>(B); // don't now if this is the way

    >
    > Dynamic cast is unecessary here, simply
    > A& base(B);
    > will work.


    Yes, but that would declare 'base' as a FUNCTION.

    >
    >> base.function(); // I need this type of call because I dont't
    >> know the derived class type at runtime.
    >> }

    >
    > This is perfectly valid.


    Nope, it wouldn't be. Try it.

    > Whether or not you want to encapsulate call
    > to the virtual function or not is your design decision, possible are
    > both ways.
    > Yours,
    > Daniel


    V
    --
    Please remove capital 'A's when replying by e-mail
    I do not respond to top-posted replies, please don't ask
    Victor Bazarov, Jun 8, 2007
    #3
  4. Javier

    Daniel Kraft Guest

    Javier wrote:
    > class A
    > {
    > public:
    > virtual void function() = 0;
    > };
    >
    > class B
    > :
    > public A
    > {
    > public:
    > void function() { cout << "Hello" << endl; };
    > }
    >
    > int main()
    > {
    > B derived();
    > A &base = dynamic_cast<A &>(B); // don't now if this is the way


    Dynamic cast is unecessary here, simply
    A& base(B);
    will work.

    > base.function(); // I need this type of call because I dont't
    > know the derived class type at runtime.
    > }


    This is perfectly valid. Whether or not you want to encapsulate call to
    the virtual function or not is your design decision, possible are both ways.

    Yours,
    Daniel
    Daniel Kraft, Jun 8, 2007
    #4
  5. Javier

    James Kanze Guest

    On Jun 8, 7:58 pm, Javier <> wrote:

    > I have a pure virtual function in the base class (to force the
    > programmers of the derived classes to have this function implemented)
    > but I want to call the derived class function from a base class
    > reference:


    > class A
    > {
    > public:
    > virtual void function() = 0;
    > };


    > class B
    > :
    > public A
    > {
    > public:
    > void function() { cout << "Hello" << endl; };
    > }


    You need a semicolon here.

    > int main()
    > {
    > B derived();


    Here you declare a function called derived, which returns a B.
    I doubt that that's what you want. Maybe:

    B derived ;

    > A &base = dynamic_cast<A &>(B); // don't now if this is the way


    Presumably, you mean:

    A& base = derived ;

    You don't need the dynamic_cast, or any other type of cast (but
    what you cast must be the name of an object, not the name of a
    type).

    > base.function(); // I need this type of call because I dont't
    > know the derived class type at runtime.


    That's exactly what virtual functions (pure or not) are for.

    > }


    > Of course I could have a function2 (private) and make it pure virtual,
    > and then have function() in base class calling the virtual function2:


    > class A
    > {
    > public:
    > void function() { this->function_impl() };
    > protected:
    > virtual void function_impl() = 0;
    > };


    That's the usual solution, in order to be able to add code
    enforcing pre- and post-conditions. If the function doesn't
    have any pre- and post-conditions (often the case when inversion
    of the control is involved), then there's no point.

    --
    James Kanze (Gabi Software) email:
    Conseils en informatique orientée objet/
    Beratung in objektorientierter Datenverarbeitung
    9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
    James Kanze, Jun 8, 2007
    #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. Replies:
    1
    Views:
    378
    myork
    May 23, 2007
  2. Replies:
    1
    Views:
    369
    Victor Bazarov
    May 23, 2007
  3. George2
    Replies:
    0
    Views:
    332
    George2
    Mar 17, 2008
  4. prabhakar732

    how call Derived class function in base class

    prabhakar732, Dec 19, 2008, in forum: C Programming
    Replies:
    2
    Views:
    428
    prabhakar732
    Dec 22, 2008
  5. Lionel B
    Replies:
    8
    Views:
    323
    James Kanze
    Feb 5, 2009
Loading...

Share This Page