Re: [Q] how to type case from POSITION to int?



The place where this makes sense is if you are the implementor of the abstraction of
POSITION. For example, I might store something in an array, although I don't want that
known outside my module. So I'll cast the index into a POSITION type and return it; the
caller does not need to know what I had there. I then implement a
Something * GetNextSomething(POSITION & p)
in the traditional MFC sense, and this allows the caller to iterate over my data without
knowing how I implemented it. If I later change the representation to a CMap, CList,
etc., the iteration is completely transparent to the clients of the module.

for example:

protected:
CArray<Something *, Something *>mydata;

public:
POSITION MyStuff::GetFirstSomethingPosition()
{
return (POSITION)0;
}

Something * MyStuff::GetNextSomething(POSITION & p)
{
if((int)p >= mydata.GetSize())
return NULL; // nothing left in the array
int n = (int)p;
p = (POSITION)(n + 1);
return mydata[n];
}

Of course, this only makes sense if you really ARE the implementor of an abstraction that
needs to return a POSITION. Otherwise, as you point out, it would be a serious
programming error.

Since the OP was amazingly vague about what constituted a "warning message" I can't
determine exactly what the correct solution to the problem is. Most likely, the index
should have been an INT_PTR or UINT_PTR instead of int.
joe

On Sun, 28 Aug 2005 07:17:22 +1000, Ian Semmel <isemmel@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx>
wrote:

>Actually, you shouldn't try. A POSITION is a POSITION and you cannot make any
>assumptions about what its structure is.
>
>The trouble with C++ is that it allows C code to be included which in turn
>allows all the hacks and bad programming practices of C to be carried over.
>
>Back 9 wrote:
>> Hello All,
>>
>> Does anyone know how I can do it without any warning messages?
>>
>> TIA
Joseph M. Newcomer [MVP]
email: newcomer@xxxxxxxxxxxx
Web: http://www.flounder.com
MVP Tips: http://www.flounder.com/mvp_tips.htm
.



Relevant Pages

  • Re: Reinitialising random number generator
    ... the PUT array in different ways, even if the size happens to be the same. ... treated by the call is up to the implementor. ... routine init_random_seed) is standard Fortran 95. ... - The quality and variation of the implementation of the PRNG. ...
    (comp.lang.fortran)
  • Re: naming conventions for columns, primary and otherwise
    ... kangaroos is a "Mob", but that word is so weird that people would miss ... What we are modeling at that level of abstraction is a set ... Think of an array as a container in the abstract, ... Would you name a Sodoku array "SodokuGrid" or some name ...
    (comp.databases)
  • Re: Surprise in array concatenation
    ... >> Slices should slide to the lower bound. ... The Ada rule breaks ... >> abstraction: ... > So if I pass a subprogram a slice of an array indexed by an enumeration type ...
    (comp.lang.ada)
  • Re: Code Comprehension
    ... why you think C arrays provide a higher level of abstraction ... And I think the pointer version is easier to read than ... The array version says "I have an array, ... anymore that you are processing the characters of a string. ...
    (comp.programming)
  • Re: Surprise in array concatenation
    ... So if I pass a subprogram a slice of an array indexed by an enumeration type ... > Inside the body of P, X is just a String -- we don't know ... the abstraction that is important here is that of the array ... consistent in Ada as possible. ...
    (comp.lang.ada)