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