473,385 Members | 1,927 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,385 software developers and data experts.

Storing objects in a union

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:

class Cell {
union {
char* s ; //std::string s ;
long l ;
double d ;
void *t ;//Table* t ;
}cell ;

......
};
I know objects (with a copy constructor) cannot be stored directly in a
union (anyone knows the *technical* reasons why?). I am "hacking" around
this by storing a pointer to the object instead - is there anything I am
overlooking (i.e. is this dangerous?, any gotcha's I should be aware
of?) - if yes, I would appreciate an alternative solution to allow me to
stor objects (or their pointers) in a union.
I

Jul 23 '05 #1
3 2222
> I know objects (with a copy constructor) cannot be stored directly in a
union (anyone knows the *technical* reasons why?).


An object with any kind of non-trivial initialization or destruction
can't be stored in a union. That includes objects with ANY kind of
constructor, objects with members with constructors, objects with
destructors, or objects that contain virtual functions. These require
the object to be constructed using a constructor, which does extra
stuff behind the scenes. It is assumed that that extra stuff is
required and can't be skipped. If you have more than one object with
non-trivial initialization in the union, who's constructor should be
called? How does an object know whether or not its destructor should
be called? etc.

Using a union to store data often isn't the best idea. Ideally, you'd
have ONE data type (perhaps std::string) to represent all the different
kinds of data, then do conversions between std::string and int, or what
else, as needed.

Another solution would be to use polymorphism. Have a generic object
base class, then derive a stringObject, intObject, etc from it.
However, then you have the problem: how do you get at the data and how
do you know what kind of data you have? Well, then you introduce
dynamic_cast, which probably isn't the most elegant solution.

There's also boost::any, (www.boost.org) which is similar to the second
solution.

Jul 23 '05 #2

Alipha wrote:
I know objects (with a copy constructor) cannot be stored directly in a
union (anyone knows the *technical* reasons why?).


An object with any kind of non-trivial initialization or destruction
can't be stored in a union. That includes objects with ANY kind of
constructor, objects with members with constructors, objects with
destructors, or objects that contain virtual functions. These require
the object to be constructed using a constructor, which does extra
stuff behind the scenes. It is assumed that that extra stuff is
required and can't be skipped. If you have more than one object with
non-trivial initialization in the union, who's constructor should be
called? How does an object know whether or not its destructor should
be called? etc.

Using a union to store data often isn't the best idea. Ideally, you'd
have ONE data type (perhaps std::string) to represent all the different
kinds of data, then do conversions between std::string and int, or what
else, as needed.

Another solution would be to use polymorphism. Have a generic object
base class, then derive a stringObject, intObject, etc from it.
However, then you have the problem: how do you get at the data and how
do you know what kind of data you have? Well, then you introduce
dynamic_cast, which probably isn't the most elegant solution.

There's also boost::any, (www.boost.org) which is similar to the second
solution.


On the same Boost site, there is a library for a variant type which
would probably be better suited for this kind of problem. The Boost
variant library uses templates and compile-time type transformations --
a technique that minimizes runtime overhead while providing a greater
margin of safety than a solution that relied on identifying dynamic
types.

Greg

Jul 23 '05 #3
Greg wrote:
On the same Boost site, there is a library for a variant type which
would probably be better suited for this kind of problem.


Have you actually used it or is that just a conjecture?

Jul 23 '05 #4

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

Similar topics

14
by: mjkahn | last post by:
I've read (and read!) that you shouldn't store objects in Session variables. I've read these reasons: - The object takes up memory that may not be freed until the session times out. Better to...
6
by: Alfonso Morra | last post by:
I have written the following code, to test the concept of storing objects in a vector. I encounter two run time errors: 1). myClass gets destructed when pushed onto the vector 2). Prog throws a...
12
by: Alfonso Morra | last post by:
I have the ff code for testing the concept of storing objects: #include <vector> #include <iostream> using namespace std ; class MyClass { public: MyClass(){
10
by: Diego F. | last post by:
Hello. I need to store custom objects in a SQL Server 2000 table. Which is the easiest way to do it? Do I need to write methods to store each attribute separately from C# app to the table and the...
4
by: Frank Rizzo | last post by:
In classic ASP, it was considered a bad idea to store VB6-created objects in the Application variable for various threading issues. What's the current wisdom on storing objects in the Application...
10
by: Mark Rae | last post by:
Hi, This relates to the previous thread "Disappearing Sessions", but is a bit more generic so I thought I'd start a new thread. This one relates to the storing of objects in Session once only to...
9
by: david | last post by:
I have a class with some business-logic and with every roundtrip, I need an instance of this class, so I have to create it, every time again. That doesn't seem very efficient. I thought it would...
1
by: Miesha.James | last post by:
Hello, I'm trying to rewrite visual c++ code into visual c++ .NET code and I've run across a problem with storing objects into a list. Here;s an example of the code I have: ref struct...
10
by: nayden | last post by:
I started playing with python a few weeks ago after a number of years of perl programming and I can say that my first impression is, unsurprisingly, quite positive. ;) The reason I am writing here...
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:
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.