Static on Multiple Inheritances?

Discussion in 'C++' started by Immortal Nephi, Apr 17, 2009.

  1. Multiple Inheritances can contain multiple diamond shapes. Can CPU be
    very overhead if two indirections and one direct memory are used?

    For example.

    Create one base class

    class A {};

    Create tens of derived classes

    class B1 : virtual public A {};
    class B2 : virtual public A {};
    .....
    .....
    class B20 : virtual public A {};
    class B21 : virtual public A {};

    Create hundreds of derived classes

    class C100 : public B1, public B2, .... public B20, public B21 {};
    class C101 : public B1, public B2, .... public B20, public B21 {};
    .....
    .....
    class C200 : public B1, public B2, .... public B20, public B21 {};
    class C201 : public B1, public B2, .... public B20, public B21 {};

    Finally, create one last derived class
    class D : public C100, public C101, .... public C200, public C201
    {
    public:
    void run() {}
    };

    int main()
    {
    D d;
    d.run();

    return 0;
    }

    You can notice that class D's vtable grows very huge with thousands of
    function members and data members. Only client can invoke d.run()
    before class D does its own job to invoke thousands of function
    members. Class D can be written into library and client includes
    library into his source code.

    C++ Compiler can compile without any problems if you are very careful
    to design and avoid prone errors. CPU's overheads can be very
    expensive. It can slow performance while class D is running at run
    time.

    If you declare static to all one base class and hundreds of derived
    classes like singleton, then hundreds of object codes are linked
    together into execution program. It can run faster because it uses
    only one direct memory without having to use twice indirection with
    huge vtable.

    Is it worth good design? You don't like to read over millions of
    lines in one file scope module. Multiple file scope modules help to
    be readability and reduced prone errors.

    Thanks...
     
    Immortal Nephi, Apr 17, 2009
    #1
    1. Advertising

  2. Immortal Nephi wrote:
    > Multiple Inheritances can contain multiple diamond shapes. Can CPU be
    > very overhead if two indirections and one direct memory are used?
    >
    > For example.
    >
    > Create one base class
    >
    > class A {};
    >
    > Create tens of derived classes
    >
    > class B1 : virtual public A {};
    > class B2 : virtual public A {};
    > ....
    > ....
    > class B20 : virtual public A {};
    > class B21 : virtual public A {};
    >
    > Create hundreds of derived classes
    >
    > class C100 : public B1, public B2, .... public B20, public B21 {};
    > class C101 : public B1, public B2, .... public B20, public B21 {};
    > ....
    > ....
    > class C200 : public B1, public B2, .... public B20, public B21 {};
    > class C201 : public B1, public B2, .... public B20, public B21 {};
    >
    > Finally, create one last derived class
    > class D : public C100, public C101, .... public C200, public C201
    > {
    > public:
    > void run() {}
    > };
    >
    > int main()
    > {
    > D d;
    > d.run();
    >
    > return 0;
    > }
    >
    > You can notice that class D's vtable grows very huge with thousands of
    > function members


    I assume each of your class B defines a new virtual function. Otherwise,
    the vtable is not likely to grow.

    > and data members.


    Data members are not part of vtable.

    > Only client can invoke d.run()
    > before class D does its own job to invoke thousands of function
    > members. Class D can be written into library and client includes
    > library into his source code.
    >
    > C++ Compiler can compile without any problems if you are very careful
    > to design and avoid prone errors.


    I am not sure "careful" is the adjective I would use to qualify such a
    design even as an exercise.

    > CPU's overheads can be very
    > expensive. It can slow performance while class D is running at run
    > time.


    Why ? Access to vtable is constant time, the size should not modify
    anything (depending on the implementation of vtable by the compiler).

    And it is unlikely that the vtable will be even used. At least, not in
    the example you give: each object is identified and the compiler will
    use the proper function call.

    > If you declare static to all one base class and hundreds of derived
    > classes like singleton, then hundreds of object codes are linked
    > together into execution program.


    But you have only one instance of them. Not what one usually wants.

    > It can run faster because it uses
    > only one direct memory without having to use twice indirection with
    > huge vtable.


    No.

    > Is it worth good design?


    No.

    > You don't like to read over millions of
    > lines in one file scope module.


    That's not what inheritance is about.

    > Multiple file scope modules help to
    > be readability and reduced prone errors.


    No it doesn't. It serves other qualities such as modularity,
    separability or configurability.

    > Thanks...


    What is your question about diamond shape inheritance ?

    --
    Michael
     
    Michael DOUBEZ, Apr 17, 2009
    #2
    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. Natan

    Static vs. non-static connection

    Natan, May 24, 2004, in forum: ASP .Net
    Replies:
    8
    Views:
    7,362
    Sami Vaaraniemi
    May 26, 2004
  2. Ben
    Replies:
    3
    Views:
    444
  3. Paul W

    Static is REALLY Static!

    Paul W, May 3, 2005, in forum: ASP .Net
    Replies:
    2
    Views:
    384
  4. Thomas Fischer
    Replies:
    3
    Views:
    546
    Henry S. Thompson
    Sep 9, 2005
  5. rickman
    Replies:
    5
    Views:
    446
    rickman
    Mar 30, 2013
Loading...

Share This Page