When you write a program that, for many cases, it is difficult to find an optimal solution can not, a significant portion are the best things you can do that.
For example, a class that is using the Create function.
Create function with classes that are used in the following cases.
A. As a final check for errors when you want and give class points
Two. If the virtual class
If a single bit more detail about the do's look.
For example, a file (image) texture reads to the class I want to make it if you say, a typical class would look like the following form.
class GxSourceTexture: public GxTexture
{
public:
GxSourceTexture ();
virtual ~ GxSourceTexture ();
bool Load (const char * filename);
;}
How do I use this class as follows:'ll. That a thorough check for errors, if
GxSourceTexture * pkSourceTexture = new GxSourceTexture ();
if (pkSourceTexture == 0)
{
error processing;
}
if (! pkSourceTexture-> Load ("image.dds"))
{
error processing;
delete pkSourceTexture;
}
Rather complicated to handle errors when reading from a file every time you must GxSourceTexture.
If this case is easier processed as follows.
class GxSourceTexture: public GxTexture
{
public:
static GxSourceTexture * Create (const char * filename);
protected:
GxSourceTexture ();
virtual ~ GxSourceTexture ();
bool Load (const char * filename);
;}
GxSourceTexture * GxSourceTexture :: Create (const char * filename)
{
GxSourceTexture * pkThis = new GxSourceTexture ();
if (pkThis == NULL)
return NULL;
if (pkThis-> Load (filename))
return pkThis;
delete pkThis;
return NULL;
}
If you want to create within the program GxSourceTexture
GxSourceTexture * pkSourceTexture = GxSourceTexture :: Create ("image.dds");
if (pkSourceTexture == NULL)
{
error processing;
}
Program is much cleaner, no? Likewise if you want to check for errors when you create, Create () function is a good start seizing as static. This method is a technique used very much.
Now if you look at the second case,
Renderer for example, to design, I made the DX9 and OpenGL renderer would like to turn on both. In this case, the virtual class will be used. When you use a virtual class, Create function, we recommend that you use.
Now if, for example, to program ...
class GxDX9Renderer: public GxRenderer
{
GxDX9Renderer ();
virtual ~ GXDX9Renderer ();
;}
class GXOGLRenderer: public GxRenderer
{
GXOGLRenderer ();
virtual ~ GxOGLRenderer ();
;}
class GxRenderer
{
GxRenderer ();
virtual ~ GxRenderer ();
static GxRenderer * Create ();
;}
Create () function does not use, supported After checking the renderer must use the class in the program. But the Create () function, and then finally in the renderer GxRenderer If you hand pointer, program entry and a more compact, GxRenderer.h can work, including the header files only.
In particular, the virtual class does not provide the actual source for the purpose of that, if doing so in the application layer and GxOGLRenderer GxDX9Renderer class header file for the class does not have to know the existence of, nor, to be added to the Renderer class in the library only can handle. (No need to modify the application else.)
In addition to this, Create () function may be used.
For example, If you need to ensure uniqueness. In this case, the Create () function can be used to ensure the uniqueness.
In addition, even in a shared resource management, Create () function is available. For example, using the source texture a.dds source files generated by the program that many times when, through a shared resource management, once the actual SourceTexture use, and simple manner that increases the value of the reference variable can be used. In this case, the class how to copy objects using the body to solve, but a lot of memory overhead for the class object itself exists. (The number of objects in 3-D graphics than the number of polygons is smaller memory overhead is relatively common result.)
No comments:
Post a Comment