class CCommand
{
...
static CCommand* INVALID;
...
};
How do I create an invalid pointer for debugging?
I''ve gotten in to the habit of creating a static pointer in classes that are used as resources called INVALID.
example:
So when I''m returing say a failed creation of a command I return CCommand::INVALID and the calling program can test for that. When I was a bit more immature I had thought that just returning zero was good enough, however i''ve found this to be much more intuitive, readable and you can validly return a null pointer from a function if that was what it was meant to do and it won''t be confusing.
Current however I generally do set the static definition to 0, simply because I cannot work out how to assign anything else. I''d like to assign INT_MAX to it or some other likely candidate.
Any ideas on how to do this, or how to do it better?
Many thanks
Chris Brodie
http:\\fourth.flipcode.com
Throw an exception. That way they have to check for it... or the program terminates.
Documents [ GDNet | MSDN | STL | OpenGL | Formats | RTFM | Asking Smart Questions ]
C++ Stuff [ MinGW | Loki | SDL | Boost. | STLport | FLTK | ACCU Recommended Books ]
class CCommand{public: class Invalid; CCommand() { if( failed ) throw Invalid; }};...try{ CCommand command; // do stuff}catch( CCommand::Invalid& e ){ // oops, no command... deal with it}
Documents [ GDNet | MSDN | STL | OpenGL | Formats | RTFM | Asking Smart Questions ]
C++ Stuff [ MinGW | Loki | SDL | Boost. | STLport | FLTK | ACCU Recommended Books ]
Whoops posted to soon:
CForm* CForm::INVALID = 0xFFFFFFFF;
c:\ZZZ\Visual Studio Projects\Game\Engine\Form.cpp(37) : error C2440: ''initializing'' : cannot convert from ''unsigned int'' to ''CForm *''
Conversion from integral type to pointer type requires reinterpret_cast, C-style cast or function-style cast
so I did this:
CForm* CForm::INVALID = (CForm*)0xFFFFFFFF;
c:\ZZZ\Visual Studio Projects\Game\Engine\Form.cpp(37) : warning C4312: ''type cast'' : conversion from ''unsigned int'' to ''CForm *'' of greater size
Not quite as easy as I thought...
Chris Brodie
http:\\fourth.flipcode.com
CForm* CForm::INVALID = 0xFFFFFFFF;
c:\ZZZ\Visual Studio Projects\Game\Engine\Form.cpp(37) : error C2440: ''initializing'' : cannot convert from ''unsigned int'' to ''CForm *''
Conversion from integral type to pointer type requires reinterpret_cast, C-style cast or function-style cast
so I did this:
CForm* CForm::INVALID = (CForm*)0xFFFFFFFF;
c:\ZZZ\Visual Studio Projects\Game\Engine\Form.cpp(37) : warning C4312: ''type cast'' : conversion from ''unsigned int'' to ''CForm *'' of greater size
Not quite as easy as I thought...
Chris Brodie
http:\\fourth.flipcode.com
Ideally you would use the in-built ''NULL'' symbol. NULL is just another way of saying ''0'', but is specifically meant for pointers to make explicit that you are not talking about the integer ''0''. The two are interchangeable. However, you want to beware of using any value except NULL or ''0'' because both of these values are garaunteed to crash the program if you accidentally try to dereference them (i.e. there is a bug in your checking code). Any other number has undefined behavior if dereferenced improperly which tends to introduce subtle and hard to track bugs.
-D
-D
As the AP said, you should use NULL or 0. However, if you want to easily get the ''max int'' value I would not use 0xFFFFFFFF since that''s merely the maximum for 32 bit integers. I normally use ~0U (the bitwise inverse of unsigned zero).
*sigh*
As I said before : use exceptions, that's what they've been designed for...
Alternatively, return the address of a static variable, instead of just a pointer with an arbitrary value :
Variable adresses are guaranteed to all be different.
Documents [ GDNet | MSDN | STL | OpenGL | Formats | RTFM | Asking Smart Questions ]
C++ Stuff [ MinGW | Loki | SDL | Boost. | STLport | FLTK | ACCU Recommended Books ]
[edited by - Fruny on June 29, 2002 4:59:19 AM]
As I said before : use exceptions, that's what they've been designed for...
Alternatively, return the address of a static variable, instead of just a pointer with an arbitrary value :
class Foo{private: static int INVALID_VAR;public: static int* INVALID;};int Foo::INVALID_VAR = 0;int* Foo::INVALID = &Foo::INVALID_VAR;
Variable adresses are guaranteed to all be different.
Documents [ GDNet | MSDN | STL | OpenGL | Formats | RTFM | Asking Smart Questions ]
C++ Stuff [ MinGW | Loki | SDL | Boost. | STLport | FLTK | ACCU Recommended Books ]
[edited by - Fruny on June 29, 2002 4:59:19 AM]
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement