Hi all,
I have design a new concept of container to be part of the cpo library.
In summary, those containers will break down the inserted objects into
some elementary parts.
Why do that?
Short answer: to save memory.
Long answer: Because the alignment requirement of data members
in an object could be very different, in a range from 1 byte to 16 bytes,
structs or classes use padding to provide the correct location for members.
That means an object size could be significantly greater than the sum
of its members sizes. Even more, when dealing with polymorphic objects,
the size is increased a little more due to the pointer to the table
of virtual methods.
I will try to outline the alternative solution, obviously, the implementation
will be part of the container, the user will not need to know the details.
---- Example:-----
#include <cstdlib>
#include <iostream>
#include <vector>
class output
{
public:
virtual void print(std::ostream& out) = 0;
};
class integer {
public:
integer(short a_ = 0) : a(a_) { }
short a;
};
class character {
public:
character(char c_ = 'a') : c(c_) { }
char c;
};
class data {
public:
data(double x_ = 0) : x(x_) { }
double x;
};
class my_object :
public integer,
public data,
public character,
public output
{
public:
my_object(int show = 1)
: integer(rand() % 100)
, data(1.0*rand()/RAND_MAX)
, character('a' + rand() % 10)
{
if (show) print(std::cout);
}
virtual void print(std::ostream& out)
{
std::cout << a << "\t" << x << " \t" << c << "\n";
}
};
void insert( std::vector<integer>& v_int,
std::vector<data>& v_data,
std::vector<character>& v_char,
const my_object& o)
{
v_int.push_back( o );
v_data.push_back( o );
v_char.push_back( o );
}
void iterate( std::vector<integer>& v_int,
std::vector<data>& v_data,
std::vector<character>& v_char
)
{
my_object o(0);
unsigned i;
for ( i = 0; i < v_int.size(); ++i)
{
static_cast