Re: [Q] how to type case from POSITION to int?
- From: Joseph M. Newcomer <newcomer@xxxxxxxxxxxx>
- Date: Sat, 27 Aug 2005 19:41:08 -0400
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
.
- References:
- [Q] how to type case from POSITION to int?
- From: Back 9
- Re: [Q] how to type case from POSITION to int?
- From: Ian Semmel
- [Q] how to type case from POSITION to int?
- Prev by Date: Re: Visual C++ Combo Box
- Next by Date: Re: CPreviewView
- Previous by thread: Re: [Q] how to type case from POSITION to int?
- Next by thread: Format Text (escape characters)
- Index(es):
Relevant Pages
|