• Advertisement

Archived

This topic is now archived and is closed to further replies.

Checking for non existant classes

This topic is 5067 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I have an array of class pointers and I want to check if the pointer leads to a valid class instance. I have tried using the ! operator, but this has returned false on some occasions when the object doesnt exist. Im sure there must be a simple enough method, but no amount of searches have found anything. Worship the holy trinity that is Blitz, Babes and Beers!

Share this post


Link to post
Share on other sites
Advertisement
There''s NO function that will tell you if a "random" number is actually a class, nor if it''s a functional class.

The only thing you can do is when you destroy an object in your array to set the value to NULL. This will not happen automagically.

Your check ought to work then.

Share this post


Link to post
Share on other sites
Remove the pointer and it becomes a normal class declaration. And surely there is a way to check if a class is valid or not, regardless of pointers. I can always use *classname to get the class reference.

Worship the holy trinity that is Blitz, Babes and Beers!

Share this post


Link to post
Share on other sites
quote:
Original post by Neo Genesis10
Remove the pointer and it becomes a normal class declaration. And surely there is a way to check if a class is valid or not, regardless of pointers. I can always use *classname to get the class reference.



You can check for a null pointer, but there is no way to check if a pointer points to anything sensible.

Share this post


Link to post
Share on other sites
quote:
Original post by Neo Genesis10
I have an array of class pointers and I want to check if the pointer leads to a valid class instance.


If you don''t set your pointers to 0 when you know the pointer is becoming invalid, you have no way whatsoever of knowing whether you are pointing to a valid object or not.


“Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.”
— Brian W. Kernighan

Share this post


Link to post
Share on other sites
What you''re asking for is a method by which you can take a value (one of the class pointers from your array) and see whether it is within a set of ''valid'' values (the addresses of all your class instances).

There are two ways of doing this:


  • A straight search through your ''valid'' set to find the test value. To achieve this, you''d have to have some system for keeping all your valid class pointers in a list somewhere - which would be hard to achieve in a reliable way (the closest you can get is a RegisterThisInstance(this) call in the constructors of all your objects, or something, and it breaks down with inheritance).

  • Test some property of the value to prove that it is valid. This cannot be done; your pointers will not all be prime numbers, or even numbers, or divisible by 3, or anything like that.



So it would look like you''re screwed. *However*, you can invert the second approach - test to see if it''s invalid (takes a value that a pointer can never have, such as 0). If that test doesn''t prove it to be invalid, then you can use it; it might be invalid, but unless you can come up with better tests, you''ll never know.

Keep invalid pointers set to zero at all times, and use (ptr!=0) to test validity.

Share this post


Link to post
Share on other sites
Alternately, use a language (Java) where "invalid" pointers (well... you tend to forget that they''re pointers when you program in Java... until you find that you really do need to copy some data with clone() or System.arraycopy() etc...) always are "0" (keyword ''null'').

Share this post


Link to post
Share on other sites

  • Advertisement