Deletion of Union of Pointers

S

Scott Kilpatrick

Let's say I have a hacked-up polymorphic container that holds variables
of type:

struct poly {
union {
int *i;
double *d;
complex<double> *c;
} value;
char type;
};

Each time a value gets added to the container, it mallocs a new value,
sets the type char, and sets the pointer to that new value. How, then,
do I write a destructor for poly? Do I do a switch on type and delete
value.x if it's that type? Also, how do you delete a poly if it
contains both pointers and non-pointers (non-pointers to native types
that is)?

Scott
 
B

Bernd Strieder

Hello,

Scott said:
Let's say I have a hacked-up polymorphic container that holds
variables of type:

struct poly {
union {
int *i;
double *d;
complex<double> *c;
} value;
char type;
};

Each time a value gets added to the container, it mallocs a new value,

It is "new", not "malloc", isn't it.
sets the type char, and sets the pointer to that new value. How, then,
do I write a destructor for poly? Do I do a switch on type and delete
value.x if it's that type? Also, how do you delete a poly if it
contains both pointers and non-pointers (non-pointers to native types
that is)?

There has to be a matching delete for every new you do. Anything else
will cause problems. You will have a hard time without switch on the
type field in the destructor.

But this is crude. Consider using OO techniques.

Introduce an abstract base class polybase, which will reduce poly to a
simple pointer. To be able to do this, replacing type fields by
inheritance, was one driving force behind the development of C++. In
terms of design pattern poly ends up as proxy class to the real poly,
which can be implemented in many ways.

A lot of sensible member functions and constructors are still missing.

class polybase
{
public:
virtual ~poly() = 0;
char type() = 0; // could be left out

// add appropriate interface

}

class intpoly: public polybase
{

char type() { return 'i';}
// implement interface
int i;
}

class doublepoly: public polybase
{

// implement interface
char type() { return 'd';}
double d;
}

struct poly // maybe : public polybase
{
~poly() { delete pb; }
char type() { return pb->type(); }
polybase *pb;
}

It might be possible to replace poly by a smart pointer.

This won't save you anything in numbers of allocations and
deallocations.


Bernd Strieder
 
M

Milburn.Young

Save yourself a little time (if you choose Bernd Strieder's technique)
and make the derived classes from a class template.

template<typename TYPE, char TYPEID>
class PolyT: public PolyBase
{
private: TYPE *t;
// implement interface
public: char getTypeID( void )
{
return( TYPEID );
}
};
typedef PolyT<int, 'i'> PolyInt;
typedef PolyT<double, 'd'> PolyInt;
typedef PolyT<complex<double>, 'c'> PolyInt;

On a design note: if this is for coefficients of polynomials, you could
store all of them as 'complex<double>'s since '2.0 + 0.0i' is the same
as '2'.

Milburn Young
 
S

Scott Kilpatrick

Bernd,

Thank you for your suggestion! I have implemented that, and apart from
a few kinks in the usage, it is working spectacularly.

Milburn,

I wish I would have seen your message sooner ;). Macros kept coming to
mind as a means of shortening the code -- I didn't even think about
templates. Thanks!

Scott
 
B

Bernd Strieder

Hello,

Scott said:
Thank you for your suggestion! I have implemented that, and apart from
a few kinks in the usage, it is working spectacularly.

The only recommendation left is applying design pattern, which might
remove those kinks in usage. Not using factory methods for example in
your context, would be almost unnatural.

Bernd
 
S

Scott Kilpatrick

What do you mean by factory methods? Also, is there any way of getting
around using methods like "geti" and "getd", or is that a necessary
part of polymorphic containers in C++? I tried at least templating the
get methods, but I couldn't make them virtual in the polybase class.

Scott
 
J

Jeff Flinn

Scott said:
What do you mean by factory methods? Also, is there any way of getting
around using methods like "geti" and "getd", or is that a necessary
part of polymorphic containers in C++? I tried at least templating the
get methods, but I couldn't make them virtual in the polybase class.

Take a look at the Variant library from www.boost.org.

Jeff Flinn
 

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,780
Messages
2,569,611
Members
45,281
Latest member
Pedroaciny

Latest Threads

Top