Re: Converting enums to pointers

From: Siemel Naran (SiemelNaran_at_REMOVE.att.net)
Date: 10/11/04


Date: Mon, 11 Oct 2004 07:22:26 GMT


"Ron Natalie" <ron@sensor.com> wrote in message news:4169e0e1$0$28244
> James Aguilar wrote:

> > const Record* EMPTY = (Record*) 0;
>
> You declare EMPTY to be a null pointer here. This is legitimate.
>
> > const Record* DELETED = (Record*) 1;
>
> This is implementation defined and may not universally work. Nothing
> guarantees you can reinterpret cast an arbitrary integer to a pointer
type.
> >
> > But if I do that I have lost all the convenience of the enumerated type.
> >
> const Record DELETED;
> would work, of course it allocates an unused Record object.

This is a fine solution, though it might be nice to use the extern keyword.
If you say

// a.cpp
const Record DELETED;
void a(std::vector<Record*>& v) {
   v.push_back(&DELETED);
}

// b.cpp
const Record DELETED;
void b(std::vector<Record*>& v) {
   v.push_back(&DELETED);
}

// main.cpp
#include "a.h"
#include "b.h"
int main() {
   std::vector<Record*>& v;
   a(v);
   b(v);
}

then even though v[0] and v[1] are both deleted records, they have different
addresses and don't compare equal (ie. because we're comparing two different
pointers).

In other words, it is not safe to compare v[N] to &DELETED or &EMPTY to see
if the record is deleted or empty.

To get around this problem we could declare DELETED and EMPTY as extern.

// Record.h
class Record { ... };
extern const Record DELETED;

// Record.cpp
#include "Record.h"
const Record DELETED;

We could also make DELETED a static member of class Record (in which case we
don't need the extern keyword as class static variables are extern anyway),
though that seems a more sylistic issue.



Relevant Pages

  • Re: why still use C?
    ... Using extern "C" is not sufficient to cover those cases. ... A function or function pointer ... library required them to send the compiler output through a "warning ... >> times the size of the thing we really want, cast the resultant pointer to ...
    (comp.lang.c)
  • Re: basic_string ctor
    ... > and a NULL pointer both construct a std::string as empty. ... than likely I'm converting something from a GUI layer to a standard ... as anything but an empty string. ...
    (microsoft.public.vc.stl)
  • Re: extern const char * vs. extern const char []http://tinyurl.com/47e3k
    ... >creates a pointer to a 'C' string literal. ... >array and it has a location, ... many platforms) since your const char* is a different type from the ... extern variables in the other TU - you need const charas the type ...
    (comp.lang.cpp)
  • Re: creating thread in C++
    ... This is defined behaviour as long as you always pass a non-NULL pointer ... to myclass together with the function pointer &myclass::threadstub to ... function and declare it extern "C". ... The 'extern "C"' directive tells the C++ tool environment to generate a call protocol for that function that's compatible with the platform's C language call protocol. ...
    (comp.programming.threads)
  • Mouse Pointer Becomes 4-Way Arrow and Wont Revert
    ... In WinXP Home SP1, my mouse pointer occasionally becomes a 4-way arrow and ... will not revert to a regular pointer unless I go to the mouse properties ... PCI 1: Empty ... Firewall: Kerio Personal Firewall 2.1.4 ...
    (microsoft.public.windowsxp.help_and_support)