Question about pure virtual functions

Discussion in 'C++' started by Sergey, Oct 31, 2005.

  1. Sergey

    Sergey Guest

    Is it possible to realize such an algorithm (this one is not working):

    class a1 {

    public:

    void af () { bf(); }
    virtual void bf () = 0;

    };

    class b1 {

    public:

    virtual void bf () { }

    };

    class c1 : public a1, public b1 { };

    main () {

    c1 x;

    }

    The general idea is in that if you have one group of classes for one
    option and another group of classes for another option you can combine
    the general class by any class from the first group and by any class
    from the second group. Question is how can functions of one group refer
    to functions of another?
    Thank you for answers.
    Sergey
    Sergey, Oct 31, 2005
    #1
    1. Advertising

  2. Sergey

    David Hilsee Guest

    "Sergey" <> wrote in message
    news:dk3npq$6ek$...
    > Is it possible to realize such an algorithm (this one is not working):
    >
    > class a1 {
    >
    > public:
    >
    > void af () { bf(); }
    > virtual void bf () = 0;
    >
    > };
    >
    > class b1 {
    >
    > public:
    >
    > virtual void bf () { }
    >
    > };
    >
    > class c1 : public a1, public b1 { };
    >
    > main () {
    >
    > c1 x;
    >
    > }
    >
    > The general idea is in that if you have one group of classes for one
    > option and another group of classes for another option you can combine
    > the general class by any class from the first group and by any class
    > from the second group. Question is how can functions of one group refer
    > to functions of another?
    > Thank you for answers.
    > Sergey


    I'm not 100% sure of what you're trying to do, but it looks like some sort
    of "mixin" where the b1 class contains the implementation for a1::bf(), but
    it does not inherit from a1, and c1 must somehow "combine" the relationship
    between the two classes. You can do this with a simple, straightforward
    "override and delegate" function:

    class c1 : public a1, public b1 {
    virtual void bf() {
    b1::bf();
    }
    };

    Here, c1::bf() is invoked, and it calls b1::bf().

    --
    David Hilsee
    David Hilsee, Oct 31, 2005
    #2
    1. Advertising

  3. Sergey

    John Carson Guest

    "Sergey" <> wrote in message
    news:dk3npq$6ek$
    > Is it possible to realize such an algorithm (this one is not working):
    >
    > class a1 {
    >
    > public:
    >
    > void af () { bf(); }
    > virtual void bf () = 0;
    >
    > };
    >
    > class b1 {
    >
    > public:
    >
    > virtual void bf () { }
    >
    > };
    >
    > class c1 : public a1, public b1 { };
    >
    > main () {
    >
    > c1 x;
    >
    > }
    >
    > The general idea is in that if you have one group of classes for one
    > option and another group of classes for another option you can combine
    > the general class by any class from the first group and by any class
    > from the second group. Question is how can functions of one group
    > refer to functions of another?
    > Thank you for answers.
    > Sergey


    You can give them a common base class and move the virtual function to it:

    #include <iostream>
    using namespace std;

    class Base
    {
    public:
    virtual void bf () = 0;
    };


    class a1 : virtual public Base
    {
    public:
    void af () { bf(); }
    };

    class b1 : virtual public Base
    {
    public:
    virtual void bf ()
    {
    cout << "This is the b1 override of bf\n";
    }
    };

    class c1 : public a1, public b1 { };

    int main ()
    {
    c1 x;
    x.af();
    }


    --
    John Carson
    John Carson, Oct 31, 2005
    #3
    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. IK
    Replies:
    2
    Views:
    609
    hemraj
    Jul 23, 2004
  2. Todd Aspeotis
    Replies:
    3
    Views:
    460
    Kanenas
    May 30, 2005
  3. John Goche
    Replies:
    10
    Views:
    742
    Marcus Kwok
    Dec 8, 2006
  4. Replies:
    7
    Views:
    589
    James Kanze
    May 2, 2007
  5. a
    Replies:
    7
    Views:
    363
    dasjotre
    Jun 28, 2007
Loading...

Share This Page