Hi-
I've got a C question...
I am implementing a database API of sorts, and am trying to be very
'clean' about the way it's done. So I want to have access routines
for the DB data which return pointers to the data such that the
progam can't accidentally modify data if it's requested in READ-ONLY
mode. I though I could do it with const declarations, but am having
trouble. Here's what i've got:
// read-only access routine; returns a POINTER to the beginning of
the data along with the SIZE of the data
dbErrType DBGetCellPtr_RO(DBOpenDBInfoType* gdb, char* colName, ULong
row, ULong* dataSize, const void** dataP);
// the const declaration makes this line illegal:
**dataP = 5; // can't assign data to const
// but that's not terribly useful b/c I will be RETURNING this ptr to
the caller
// client application using Database.c. I don't want it to be able to
MODIFY the contents returned by the above call:
byte* dataP;
dbErr = DBGetCellPtr_RO(gdb, "name", 0, &dataSize, (void**) &dataP);
Problem is, that the compiler lets me do this! Which means that I can
also do this:
*dataP = 5; // legal, but undesired!
I was hoping that it would force me to do this:
const void* dataP;
dbErr = DBGetCellPtr_RO(gdb, "name", 0, &dataSize, &dataP);
*dataP = 5; // illegal; dataP is a ptr to const
---
Of course I realize that I could just declare my variables as const
to prevent them from using things illegally, but I really wanted to
be 'clean' about it and have the compiler keep me honest!
Any advice on dealing with this situation would be GREATLY
appreciated (read - Alan will buy you some beers at PalmSource '99 :).
Alan Pinstein
Synergy Solutions, Inc.
http://www.synsolutions.com
1-800-210-5293