Pointers- Confused and hating them

Alan Smith alanrogersmith at gmail.com
Wed Jan 2 18:08:36 PST 2008


Hello everyone!

I am trying to create a cleaner version of dstring. My version, like the original, uses an union to store the string.

union
{
 char* _cptr;
 wchar* _wptr;
 dchar* _dptr;
}

I tried to transition from Objective-C to C++ so I could write cross-platform code. Objective-C can use pointers but I didn't ever need to. Now that I am trying to duplicate the small size of dstring with pointers, the way dstring does it, I'm getting lost. There are a couple things I need to know about using unions and pointers.

#1 How do I figure out which of the three members in the union are used? I tried comparing each to null (if (_cptr !is null) return _cptr;...) but that didn't work, they weren't null, the wrong one had a small piece of the string in it. My test got worse and worse but now finally works. Here is one of the ifs:

if (*_cptr !is 0)
// _cptr is the one I want

I consider that ugly and would like to know if there is a better way to do it.

#2 How do I change the pointer I'm using from the union? I want to change from one union member to another. For example, the current string has a char[] type. I then alter one of the characters (e.g. str[0] = 'ൠ') and so the type needs to be changed to wchar[]. How should I do this? How should I change the union member I am using?

Many thanks, I have been working on this all day and have done lots of archive searching to no avail. You're help will be *greatly* appreciated!

Peace, Alan



More information about the Digitalmars-d mailing list