Jump to content

  • Log In with Google      Sign In   
  • Create Account


#ActualServant of the Lord

Posted 31 March 2013 - 01:37 PM

Named constructor idiom, as one example.

 

class Shape
{
     Shape(); //Default constructor.
     Shape(VertexArray vertices); //Create a shape from a list of points.
 
     static Shape CreateRectangle(x, y, width, height); //Convenience function to create and return a shape.
};
 
Shape shape(......);
Shape myRectangle = Shape::CreateRectangle(x, y, width, height);

 

In my own code-base oftentimes I have static member variables that are shared by every instance of that class (for example, "DefaultTileImage" of a Tile class).

Due to API requirements, I can't load images until after the API has been initialized and a window created, so I can't initialize and load DefaultTileImage in the source file:

Image Tile::DefaultTileImage = NULL; //I can't load it here, because of the API requirements.

 

So instead, I have a static function to load the Tile's static DefaultTileImage, after the API has been initialized.

...API is initialized...
...game is being initialized...
Tile::LoadDefaultTileImage("image");

#2Servant of the Lord

Posted 31 March 2013 - 01:33 PM

Named constructor idiom, as one example.

 

class Shape
{
     Shape(); //Default constructor.
     Shape(VertexArray vertices); //Create a shape from a list of points.
 
     static Shape CreateRectangle(x, y, width, height); //Convenience function to create and return a shape.
};
 
Shape shape(......);
Shape myRectangle = Shape::CreateRectangle(x, y, width, height);

#1Servant of the Lord

Posted 31 March 2013 - 01:33 PM

Named constructor idiom, as one example.

class Shape
{
     Shape(); //Default constructor.
     Shape(VertexArray vertices); //Create a shape from a list of points.
 
     static Shape CreateRectangle(x, y, width, height); //Convenience function to create and return a shape.
};
 
Shape shape(......);
Shape myRectangle = Shape::CreateRectangle(x, y, width, height);

PARTNERS