Described the "named constructor idiom"?, C/C++ Programming

Described the "Named Constructor Idiom"?

Posted Date: 3/15/2013 6:18:00 AM | Location : United States

 A: A method which provides more intuitive and/or safer construction operations for users of your class.

The difficulty is that constructors have the same name always as the class. Thus the only way to differentiate among the various constructors of a class is via the parameter list. But if there are many constructors, the differences among them become somewhat and error prone and subtle.

Along the Named Constructor Idiom, you say publicly all the class''s constructors in protected or private sections, and you provide public static methods which return an object. These static techniques are "Named Constructors." usually, there is one such static method for each distinct way to construct an object.

For instance, suppose we are creating a Point class which represents a position on the X-Y plane. Turns out there are two common ways to mention a 2-space coordinate: polar coordinates (Radius+Angle), rectangular coordinates (X+Y). Unluckily the parameters for these two coordinate systems are the alike: two floats. It would create an ambiguity error in the overloaded constructors:

class Point {


Point(float x, float y); // Rectangular coordinates                              

Point(float r, float a); // Polar coordinates (radius and angle)

// ERROR: Overload is Ambiguous: Point::Point(float,float)


int main()


Point p = Point(5.7, 1.2); // Ambiguous: Which coordinate system?



One way to solve out this ambiguity is to employ the Named Constructor Idiom:

#include // To get sin() & cos()

class Point {


static Point rectangular(float x, float y); // Rectangular coord''s static Point polar(float radius, float angle); // Polar coordinates

// These static methods are so-called "named constructors"

... private:

Point(float x, float y); // Rectangular coordinates float x_, y_;


inline Point::Point(float x, float y)

: x_(x), y_(y) { }

inline Point Point::rectangular(float x, float y)

{ return Point(x, y); }

inline Point Point::polar(float radius, float angle)

{ return Point(radius*cos(angle), radius*sin(angle)); }

The users of Point now have a clear & unambiguous syntax for developing Points in either coordinate system:

int main()


Point p1 = Point::rectangular(5.7, 1.2); // clearly rectangular

Point p2 = Point::polar(5.7, 1.2); // Obviously polar



Ensure your constructors are in protected section if you expect Point to contain derived classes.

The Named Constructor Idiom can also be utilized to make sure your objects are always created using new.

Note down that the Named Constructor Idiom, at least as implemented above, is only as fast as calling directly constructor modern compilers will not make any additional copies of your object.


Posted by | Posted Date: 3/15/2013 6:19:37 AM

Related Discussions:- Described the "named constructor idiom"?, Assignment Help, Ask Question on Described the "named constructor idiom"?, Get Answer, Expert's Help, Described the "named constructor idiom"? Discussions

Write discussion on Described the "named constructor idiom"?
Your posts are moderated
Related Questions
THIS PROGRAM IS TO COMPARE THE TWO DATES GIVEN BY THE USER #include #include #include struct date  {   int dd;   int mm;   int yy;  }; date compare(date d1,date d2)  {

progarm in c for area under curve   #include float start_point, /* GLOBAL VARIABLES */ end_point, total_area; int numtraps;

What does it mean to declare a 1.      member function as a virtual A: (a) C++ virtual function is member function of any class, whose functionality may be over- ridden in

a program to accept average, if average is 75 to 85 print it out else bypass using counter. enter and process 5 records

In this problem you are given a board in which some of the elements are placed as shown in diagram below. Each element represents a color. Fill the other elements in the board, suc

Write a program to find the area under the curve y = f(x) between x = a and x = b, integrate y = f(x) between the limits of a and b. The area under a curve between two points can b

My file pointer is going to the location which i specified and writing the value but it is in next line so,please help me iam developing in visual studio6

C Program for TOTAL  & AVERAGE IN THE ARRAY  #include stdio.h> #include conio.h> #include string.h> void main() {           int i=0,n[100],limit=0,a=0;