By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,922 Members | 1,185 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,922 IT Pros & Developers. It's quick & easy.

Storing objects in a union

P: n/a
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
Share this Question
Share on Google+
3 Replies


P: n/a
> 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

P: n/a

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

P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.