Question

My idea is to print the object it points to. I think a valid pointer should have a valid object. If we try to print out the object we verify if the pointer is valid. Am I right?

Was it helpful?

Solution

I think a valid pointer should have a valid object.

Yes, that's the definition of a valid pointer.

If we try to print out the object we verify if the pointer is valid.

Unfortunately, you can't. You can check whether the pointer is null; but if it wasn't initialised properly, or if it pointed to an object that's been destroyed, it will be neither valid nor null.

If you want a pointer that's smart enough to know whether it's valid, you'll need a smart pointer.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top