473,378 Members | 1,539 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

union and polymorphism

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

Oct 11 '07 #1
3 4202
Pe*******@gmail.com wrote:
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
Oct 11 '07 #2
Kai-Uwe Bux wrote:
Pe*******@gmail.com wrote:
>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
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
Oct 12 '07 #3
Kai-Uwe Bux wrote:
Pe*******@gmail.com wrote:
>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
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
Oct 12 '07 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

37
by: Mike Meng | last post by:
hi all, I'm a newbie Python programmer with a C++ brain inside. I have a lightweight framework in which I design a base class and expect user to extend. In other part of the framework, I heavily...
4
by: jonnychang | last post by:
What is the purpose of using union in this class? Class A { public: union { int age; char * name; double amount; } };
3
by: Ann Huxtable | last post by:
Hi, I am writing a nested table structure to mimic data returned from a database (actually, a heirarchical recordset). I am representing the cells where the actual data is stored, by a union:...
2
by: balasam | last post by:
I want what is the application of union in C.Please explain
2
by: sarathy | last post by:
Hi all, I need a small clarification reg. Templates and Polymorphism. I believe templates is really a good feature, which can be used to implement generic functions and classes. But i doubt...
84
by: Peter Olcott | last post by:
Is there anyway of doing this besides making my own string from scratch? union AnyType { std::string String; double Number; };
11
by: chsalvia | last post by:
I've been programming in C++ for a little over 2 years, and I still find myself wondering when I should use polymorphism. Some people claim that polymorphism is such an integral part of C++,...
17
by: Bart Friederichs | last post by:
Hello, I created the following inheritance: class Parent { public: void foo(int i); }; class Child : public Parent {
7
by: Peter Olcott | last post by:
Why can a union have a member with a copy constructor?
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.