Global Pointers

hey....
i want to use a pointer that i can access in my class as well as in the main()....can anybody explain me how to define global pointer???
Make the pointer static in the class (make sure it is public), and then use ClassName::pointer to access it, where 'ClassName' is the name of the class and 'pointer' is the pointer. Be aware that there is only one pointer. If you want to create multiple instances of the class, the pointer for each instance will be exactly the same because ClassName is the actual class name. After all, the idea of class_instance.pointer seems pretty fishy to me when class_instance2.pointer, class_instance3.pointer, etc. would all have the same value because it is static. That is why you use the actual class name.

The other option is to literally define a global pointer. You just declare the pointer before the class implementation, and before main().

I personally would go the static route, but then again, I can't think of a reason for using a pointer like that. :P
Last edited on
Topic archived. No new replies allowed.