Skip to main content
GameDev.net gamedev.net
🔒 Locked

Class, Pointer question

Started by Metal Typhoon May 10, 2003 at 12:44 AM 8 replies 350+ views
Original Post
Metal Typhoon
Metal Typhoon
i have the following class. and i want to be able to use the -> operator evrytime i need to acess something there... how can this be done another way without doing a typedef classname* othername after a class definition ? and if this is possible wheni''m implementing my class how will the function def look like ? i.e : bool classname::func (); .. whould it change ? this is the class
  
class Render
{
	public:
	//Enable Opengl 

		bool Enable (); 	
	//Disables Opengl

		bool Disable (); 	 
	//Resize the scene with perspective views 

		void ResizeModelScene (int Width,int Height,float Start,float End);

	private:
		HWND	Hwnd; //Handle to a window

		HDC		Hdc;  //Handle to device context

		HGLRC	Hrc;  //Handle to rendering context

};

typedef Render* Ogl_Obj; //<---- i dont want to do this... 


  
Metal Typhoon
Zipster
Zipster
I''m not clear as to what you are trying to do, and why. The -> operator is used for when you have a pointer to your object. You could always do something like this:

(&MyObject)->func();

But I don''t see why...
Metal Typhoon
Metal Typhoon
quote:
Original post by Zipster
I''m not clear as to what you are trying to do, and why. The -> operator is used for when you have a pointer to your object. You could always do something like this:

(&MyObject)->func();

But I don''t see why...


hehe it jsut looks cooler to use the -> than the . :D so my question is.. can i somehow declare my class so when i use it it will aways be a pointer to a object ?
Metal Typhoon
Zipster
Zipster
The class declaration has nothing to do with whether or not you declare a pointer or an instance, aside from a few exceptions like pure virtual functions. You simply have to declare a pointer instead of an instance whenever you want to use your class and manage the allocation yourself (and subsequent release). Although that''s a lot of work if you don''t need to. I don''t recommend doing something based on its "coolness" factor, that usually leads to trouble.
Kippesoep
Kippesoep
You could always do something such as this:


  
class CTest
{
public :
CTest *operator -> () { return this; }
}


That''ll make the operator -> work on non-pointers, but it''s bad programming practice to do this, though. Just use . and -> where they should be used.

Kippesoep
Metal Typhoon
Metal Typhoon
quote:
Original post by Zipster
The class declaration has nothing to do with whether or not you declare a pointer or an instance, aside from a few exceptions like pure virtual functions. You simply have to declare a pointer instead of an instance whenever you want to use your class and manage the allocation yourself (and subsequent release). Although that's a lot of work if you don't need to. I don't recommend doing something based on its "coolness" factor, that usually leads to trouble.


this led me to more questioning.. where and what kind of trouble can it lead to ?

[edited by - Metal Typhoon on May 10, 2003 2:14:11 AM]
Metal Typhoon
Zipster
Zipster
Like I mentioned, you would have to handle the allocation of the object yourself, make sure the pointer is zero''d if nothing is allocated, free it once you are finished, etc. I consider it trouble because of the increased potential for something to go wrong. You might forget to free it, or zero the pointer, or even allocate it in the first place. There''s more room for bugs to form essentially. I''m not saying it''s inevitable, but you shouldn''t be making things harder on yourself. Just go with a regular instance if that''s what you need
Illumini
Illumini
This is a pretty funny thread.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.