Re: compile time efficency / run time flexibility - best of both worlds

Discussion in 'C++' started by Daniel T., Oct 18, 2004.

  1. Daniel T.

    Daniel T. Guest

    In article <>,
    Ares Lagae <> wrote:

    > Suppose one is writing a library behavior the classes A1, A2 and A3, which
    > share some common behavior. Some applications will only use one of the
    > classes, while other applications will use all of them.
    >
    > Because the classes share common behavior, the library designed has to
    > choose wether to introduce an abstract base class A, or not. Introducing an
    > ABC has the advantage of run time flexibility, but efficiency suffers
    > (virtual functions & inlining, ...).
    >
    > To have the best of both worlds, you could try this:
    >
    > class A1 // no inheritance
    > {
    > // no virtuals
    > f();
    > };
    >
    > class A2
    > {
    > f();
    > };
    >
    > class A3
    > {
    > f();
    > };
    >
    > class A
    > {
    > virtual f();
    > };
    >
    > template <class Ax>
    > class A_wrapper : public A
    > {
    > public:
    > A_wrapper(Ax& a) : a_(a) {}
    > f() { return a_.f(); }
    > private:
    > Ax a_;
    > }
    >
    > Applications that use only one of the classes:
    >
    > A2 a2;
    > a2.f();
    >
    > Applications that need run time flexibility:
    >
    > A * a = new A_wrapper<A2>(A2());
    >
    > What do you think of this library design technique ?


    Unnecessary.

    class A { virtual ~A() { } virtual void f() = 0; };

    class A1 { void f(); };

    To use only one of the classes:

    A1 a1;
    a1.f();

    The above will pre-bind. The virtual mechanism will not be used.
    Daniel T., Oct 18, 2004
    #1
    1. Advertising

  2. Daniel T.

    Ares Lagae Guest


    > Unnecessary.
    >
    > class A { virtual ~A() { } virtual void f() = 0; };
    >
    > class A1 { void f(); };
    >
    > To use only one of the classes:
    >
    > A1 a1;
    > a1.f();
    >
    > The above will pre-bind. The virtual mechanism will not be used.


    The why do new proposals (like the random number library) don't use abstract
    base classes ? Are most C++ programmers still "afraid" of virtuals ?

    best regards,
    Ares Lagae
    Ares Lagae, Oct 18, 2004
    #2
    1. Advertising

  3. Daniel T.

    Daniel T. Guest

    Ares Lagae <> wrote:

    > > Unnecessary.
    > >
    > > class A { virtual ~A() { } virtual void f() = 0; };
    > >
    > > class A1 { void f(); };
    > >
    > > To use only one of the classes:
    > >
    > > A1 a1;
    > > a1.f();
    > >
    > > The above will pre-bind. The virtual mechanism will not be used.

    >
    > The why do new proposals (like the random number library) don't use abstract
    > base classes ? Are most C++ programmers still "afraid" of virtuals ?


    Because they are out of vogue in C++ right now. Notice that very little
    of the standard library use classes that contain virtual
    member-functions. Off hand, I think that the only portion of the
    standard that does use virtuals is the IO library, which pre-dates the
    STL proposal (i.e. came into existence while classes with virtual
    member-functions were more popular.)
    Daniel T., Oct 19, 2004
    #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. me

    Databind() and flexibility?

    me, Feb 27, 2004, in forum: ASP .Net
    Replies:
    2
    Views:
    315
  2. gajo
    Replies:
    7
    Views:
    1,375
    Chris Smith
    Jun 7, 2004
  3. Zombie
    Replies:
    0
    Views:
    343
    Zombie
    Oct 8, 2004
  4. Replies:
    11
    Views:
    594
    Tor Iver Wilhelmsen
    Oct 13, 2006
  5. Jim Langston

    Best of both worlds yet?

    Jim Langston, Sep 25, 2010, in forum: C++
    Replies:
    3
    Views:
    510
    Jim Langston
    Sep 25, 2010
Loading...

Share This Page