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 {

public:

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 {

public:

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
Byteland county is very famous for luminous jewels. Luminous jewels are used in making beautiful necklaces. A necklace consists of various luminous jewels of particular colour. Nec

Description: Create a program that allows the user to track the hours an employee worked in a week. How much the employee was paid and any extra hours worked (overtime pay).

One person who is specialist at programming and solving problems with a computer Project Description: Dear Potential computer, hardware, programming and software genius, I

We are now quite happy to use the basic mathematical expressions, however in engineering we use scientific functions i.e Sin , CosĀ  , ln etc . Within C we have the following functi

The definition of an object(variable ): We can explain a variable(set memory to the variable) in the following ways. e.g. double salary; int month; When more than o

I need whatsapp software in my website Project Description: i need whatsapp software in my website same this whatsapp if anyone can make to me this in my website Skills

Given a bool variable isReadable write some statements that assign true to isReadable if the file "topsecret" exists and can be read by the program and assigns false to isR

Prepare a program to obtain prime factors of any integer number using functions

Question 1 Consider you have to find out average marks of 100 students in a class. Write simple program(s) using different iteration statements to accomplish it Question 2 Expla

Is it legal for a member function to say delete this? A: As long as you're cautious, it's OK for an object to delete this. Here's how I define "cautious": You have to be a