Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Benefits of Static Functions for Class Constructions

I have seen code like this, so I'm trying to find the reason.
What's the main reason for having static functions that act as constructors, instead of having actual constructors?

I mean something like:

class MyClass
{
    public:
    static MyClass CreateFrom( bar );
    static MyClass CreateFrom( foo );
    ...
}

instead of:

class MyClass
{
    public:
    MyClass( bar );
    MyClass( foo );
    ...
}
like image 654
MBZ Avatar asked Aug 08 '13 04:08

MBZ


3 Answers

This is called the "named constructor idiom".

It's typically used when:

  1. You have a lot of ctors with similar enough enough parameter lists that overloaded ctors would be confusing (e.g., different mixtures of integer and floating point numbers, so 1, 1.0, 1 is supposed to mean something different from 1, 1, 1.0).
  2. You have two different sources that both provide the input as the same type. For example, let's assume you wanted to convert a distance on the surface of the earth into the angle subtended between points that distance apart -- but you might want to supply the distance in either miles or kilometers, either of which would be represented as a double.

In this case a single angle(double dist) can't distinguish between input in kilometer vs. miles, but: angle_from_miles and angle_from_kilomters can do that quite easily.

like image 120
Jerry Coffin Avatar answered Oct 10 '22 02:10

Jerry Coffin


They are called Named Constructors.

The are basically used when you want to construct an object which requires to pass a particular set of parameters but internally you need to construct the object differently.

For example you have a class like:

class AREA
{
double area;
AREA(int x);
};

//how will you construct the object differently in case of circle and square??

For this purpose, we have named constructors which help to create a relevant object.

So we may create 2 static methods inside the class as:

static AREA square(int x)
{ return AREA(x*x); }

and

static AREA circle(int x)
{ return AREA(x*x*3.14); } //or a more accurate PI value

Thus, we may call the relevant static function to return the object initialized with the required area.

NOTE: These are static as while creating an object for a particular class you shouldn't be requiring an object to do so.

Check THIS for more details.

like image 40
Saksham Avatar answered Oct 10 '22 03:10

Saksham


The most obvious benefits are:

  • It's easy to specify the implementation which constructs the instance at the callsite, when multiple constructors are provided. This makes it easier for the class to provide multiple variants.
  • These variants may also have different names, but identical parameter lists (or parameter lists which the compiler may call out as ambiguous when determining which to choose).
  • It helps you because you can read which constructor your implementation calls -- at the callsite.

Another reason is that it is easier in some cases to initialize the class within a function body, rather than using an initialization list.

like image 22
justin Avatar answered Oct 10 '22 03:10

justin