I'm trying to create an octree to contain data. Each piece of data contained in the octree is about 4 bytes. I have the following implementation.
struct p_CountResult
{
unsigned int leaves;
unsigned int nodes;
};
struct o_Parent
{
o_Parent()
{
}
virtual void count(p_CountResult& _in) const
{
}
};
struct Leaf : o_Parent
{
const Type_T data_;
Leaf(const Type_T& _data, const unsigned char& _depth) : o_Parent(), data_(_data)
{
}
void count(p_CountResult& _in) const override
{
_in.leaves += 1;
}
};
struct Node : o_Parent
{
o_Parent* children[8];
Node(...) : o_Parent()
{
this->Subdivide(_depth, _targetDepth);
}
~Node()
{
for (const auto& _c : children)
delete _c;
}
void count(p_CountResult& _in) const override
{
_in.nodes += 1;
for (const auto& child : children)
{
child->count(_in);
}
}
};
As you can see I created a base class which is o_Parent, which has just one virtual function for now (might also have more in the future), but has no data at all. The reason I need this baseclass is to use polymorphism on both the call of count()
and to create an octree which can have a Node or a Leaf as a root:
template<typename Type_T>
struct Octree
{
const o_Parent* root_;
}
But using this base class makes the size of the octree much bigger than it needs to (I think), as this are the results I obtain from profiling the different sizes of the classes.
unsigned char: 1
o_Parent: 4
Type_T: 4
Leaf: 8
Node: 36
As you can see the Type_T, which is the data type, is 4 bytes, but a leaf is 8 bytes, and this causes an octree to be almost twice as big as it would be if the Leaf was just 4 bytes, which is really the amount of data I need.
Is there a way to create an empty Base Class with virtual functions? By reading online I found that c++ doesn't allow size 0 classes because for example it wouldn't be able to allocate arrays of such classes. But if I understood correctly, I'm not doing so in my code as I'm allocating an array of pointers to such class in the Node superclass, so such restriction shouldn't apply (?)
Am I doing this in the correct way or is there a better way to do what I'm trying to achieve? I thing that something that could help is someway of telling the compiler that I am never instancing the class o_Parent by itself, so that it doesn't need to worry abot allocation size (something similar to Dart's abstract
) but I haven't found a way of doing so in c++.