473,408 Members | 2,888 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,408 software developers and data experts.

Deletion of Union of Pointers

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

Feb 16 '06 #1
6 2652
Hello,

Scott Kilpatrick wrote:
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

Feb 16 '06 #2
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

Feb 16 '06 #3
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

Feb 16 '06 #4
Hello,

Scott Kilpatrick wrote:
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

Feb 17 '06 #5
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

Feb 17 '06 #6
Scott Kilpatrick wrote:
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
Feb 17 '06 #7

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

Similar topics

5
by: Ricky Lung | last post by:
struct Foo { union { int& i; float& j; }; Foo(int& val) : i((int&)val), j((float&)val) {} };
13
by: Razmig K | last post by:
Dear mates, This is an another survey for the common (and uncommon) nontrivial uses of the aforementioned C construct. It's posted by the same average C programmer who's made a similar survey...
67
by: S.Tobias | last post by:
I would like to check if I understand the following excerpt correctly: 6.2.5#26 (Types): All pointers to structure types shall have the same representation and alignment requirements as each...
56
by: ccwork | last post by:
Hi all, Here is a sample code segment: .... typedef PACKED struct { union { PACKED struct { char red:1;
3
by: A_Republican | last post by:
I am interested in writing my own secure file deletion program. I want to be able to read and write to my hard drive directly. My application will seach my hard drive for all locations marked for...
4
by: Michael Brennan | last post by:
I have a menu_item structure containing an union. func is used if the menu item should use a callback, and submenu if a popupmen should be shown. struct menu_item { enum { function, popup }...
4
by: lokki | last post by:
Hello, can anybody tell me what's wrong with following example code? char *k, *v; k = new char; strcpy(k, "a2"); v = new char;
30
by: Yevgen Muntyan | last post by:
Hey, Why is it legal to do union U {unsigned char u; int a;}; union U u; u.a = 1; u.u; I tried to find it in the standard, but I only found that
2
by: puzzlecracker | last post by:
Why Attempt #3 is Deplorable First, let's consider a few reasons why Attempt #3 above truly is deplorable form: 1. Alignment. Unlike dynamic memory acquired by ::operator new(), the x_...
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: 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
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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.