union and polymorphism

P

PengYu.UT

Hi,

I'm reading an R-tree implementation converted from C to C++. A union
is used for the internal nodes and the leaves of the tree.

union
{
Node* m_child; ///Child node, if it
is an internal node
DATATYPE m_data; ///Data, if it is a leaf.
};

My feeling is that union is not used in C++ very often. union can be
removed by using polymorphism. I'm wondering for this tree application
whether it is better to use polymorphism rather than union. When I
shall do it the other way?

Thanks,
Peng
 
K

Kai-Uwe Bux

Hi,

I'm reading an R-tree implementation converted from C to C++. A union
is used for the internal nodes and the leaves of the tree.

union
{
Node* m_child; ///Child node, if it
is an internal node
DATATYPE m_data; ///Data, if it is a leaf.
};

My feeling is that union is not used in C++ very often. union can be
removed by using polymorphism. I'm wondering for this tree application
whether it is better to use polymorphism rather than union. When I
shall do it the other way?

Unions impose severe restrictions on what can go in. In your example, the
DATATYPE must have trivial default constructor, trivial copy constructor,
and trivial destructor. Using polymorphism, you will have more flexibility
down the road.


Best

Kai-Uwe Bux
 
P

Pavel

Kai-Uwe Bux said:
Unions impose severe restrictions on what can go in. In your example, the
DATATYPE must have trivial default constructor, trivial copy constructor,
and trivial destructor. Using polymorphism, you will have more flexibility
down the road.


Best

Kai-Uwe Bux
All true. Although as long as DATATYPEs to be used satisfy all the above
requirements, the union might provide most efficient implementation in
both speed and space (the latter -- as long as a separate discriminator
is not required to distinguish leaves from internal nodes -- which is I
believe is the case with R-trees as all leaves are on the same level).
From my experience, if the purpose of C-to-C++ conversion is to add a
modern interface, as opposed to adding essential features, changing
underlying data structures rarely pays off; I would rather encapsulate C
structures into C++ classes by aggregation.

Hope this helps
-Pavel
 
P

Pavel

Kai-Uwe Bux said:
Unions impose severe restrictions on what can go in. In your example, the
DATATYPE must have trivial default constructor, trivial copy constructor,
and trivial destructor. Using polymorphism, you will have more flexibility
down the road.


Best

Kai-Uwe Bux
All true. Although as long as DATATYPEs to be used satisfy all the above
requirements, the union might provide most efficient implementation in
both speed and space (the latter -- as long as a separate discriminator
is not required to distinguish leaves from internal nodes -- which is I
believe is the case with R-trees as all leaves are on the same level).
From my experience, if the purpose of C-to-C++ conversion is to add a
modern interface, as opposed to adding essential features, changing
underlying data structures rarely pays off; I would rather encapsulate C
structures into C++ classes by aggregation.

Hope this helps
-Pavel
 

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,773
Messages
2,569,594
Members
45,119
Latest member
IrmaNorcro
Top