Static on Multiple Inheritances?

I

Immortal Nephi

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...
 
M

Michael DOUBEZ

Immortal said:
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 ?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,968
Messages
2,570,149
Members
46,695
Latest member
StanleyDri

Latest Threads

Top