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?

有帮助吗?

解决方案

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.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top