Mitsukai
June 10th, 2006, 11:38 PM
Vtables, those nasty memory block that makes each class 4 bytes bigger when it uses virtuals. There is a way to avoid them, and your compiler may even optimize it for a very little tiny bit more speed(if it optimizes virtuals diffrent than normal functions at all).
This could perhaps make pc's work faster, for each class instance it doesnt need to construct a vtable anymore, and doesnt need as much ram anymore. There could be alot of classes used with vtables in all of your applications on your pc.
Imagine a game that uses objects with vtables. It has initialized 1 000 objects and there are 3 create and 3 destroyed evry second thats up 4 000 bytes to much allocated space! and 12 bytes allocated evry second for nothing.
This can easily be accomplished by a "container" structure.
Here is a basic example:
#include <cstdio>
template<typename t_Container = Animal>
struct Animal
{
struct ACTIONS
{
static void Talk(const Animal& me)
{
me.Say("...");
}
};
void Say(const char* text) const
{
printf("%s", text);
}
void Talk(void) const
{
t_Container::ACTIONS::Talk(*this);
}
};
struct Cow: Animal<Cow>
{
struct ACTIONS: Animal::ACTIONS
{
static void Talk(const Animal& me)
{
me.Say("mooo");
}
};
};
int main(void)
{
Cow MyPet;
printf("%s", "My pet says: ");
MyPet.Talk();
while(true);
return(0);
}
Downside:
Yes there is a downside. In my code above, there is this rule: Once a cow always a cow. It cannot say ... anymore, like an animal without race would say.
results:
sizeof(Cow) returned 1, sizeof(CowV) (same class but using a virtual function) returned 4, conclusion my idea worked.
This method is VERY usefull as i stated ealier. But has its downside, but most of the time the downside shulndt be much of a big deal.
This could perhaps make pc's work faster, for each class instance it doesnt need to construct a vtable anymore, and doesnt need as much ram anymore. There could be alot of classes used with vtables in all of your applications on your pc.
Imagine a game that uses objects with vtables. It has initialized 1 000 objects and there are 3 create and 3 destroyed evry second thats up 4 000 bytes to much allocated space! and 12 bytes allocated evry second for nothing.
This can easily be accomplished by a "container" structure.
Here is a basic example:
#include <cstdio>
template<typename t_Container = Animal>
struct Animal
{
struct ACTIONS
{
static void Talk(const Animal& me)
{
me.Say("...");
}
};
void Say(const char* text) const
{
printf("%s", text);
}
void Talk(void) const
{
t_Container::ACTIONS::Talk(*this);
}
};
struct Cow: Animal<Cow>
{
struct ACTIONS: Animal::ACTIONS
{
static void Talk(const Animal& me)
{
me.Say("mooo");
}
};
};
int main(void)
{
Cow MyPet;
printf("%s", "My pet says: ");
MyPet.Talk();
while(true);
return(0);
}
Downside:
Yes there is a downside. In my code above, there is this rule: Once a cow always a cow. It cannot say ... anymore, like an animal without race would say.
results:
sizeof(Cow) returned 1, sizeof(CowV) (same class but using a virtual function) returned 4, conclusion my idea worked.
This method is VERY usefull as i stated ealier. But has its downside, but most of the time the downside shulndt be much of a big deal.