Re: Converting enums to pointers
From: Siemel Naran (SiemelNaran_at_REMOVE.att.net)
Date: 10/11/04
- Next message: JKop: "Re: Difference between int() and (int)"
- Previous message: Steven T. Hatton: "Re: Make t2 const: T1 (&aPlus(T1(&t1)[S],T2(&t2)[S]))[S]"
- In reply to: Ron Natalie: "Re: Converting enums to pointers"
- Next in thread: Rolf Magnus: "Re: Converting enums to pointers"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
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.
- Next message: JKop: "Re: Difference between int() and (int)"
- Previous message: Steven T. Hatton: "Re: Make t2 const: T1 (&aPlus(T1(&t1)[S],T2(&t2)[S]))[S]"
- In reply to: Ron Natalie: "Re: Converting enums to pointers"
- Next in thread: Rolf Magnus: "Re: Converting enums to pointers"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|