Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6320049
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T15:54:17+00:00 2026-05-24T15:54:17+00:00

I use the named constructor idiom to create objects, because I have lots of

  • 0

I use the named constructor idiom to create objects, because I have lots of calls with identical parameters but the object shall be created differently.

The C++ FAQ tell us how to do this. It also tells us how to force objects being heap allocated. Yet it really fails to tell us how to use the named constructor idiom with the new operator.

Because new requires a constructor to be called we cannot directly call named constructors. So I found two workarounds to this problem:

I create an additional copy constructor and hope that optimizing compilers won’t create a temporary object.

class point_t {
    int X,Y;
    point_t(int x, int y) : X(x), Y(y) { }
  public:
    point_t(const point_t &x) : X(x.X), Y(x.Y) { }
    static point_t carthesian(int x, int y) { return point_t(x,y); }
    static point_t polar(float radius, float angle) {
      return point_t(radius*std::cos(angle), radius*std::sin(angle));
    }

    void add(int x, int y) { X += x; Y += y; }
};



int main(int argc, char **argv) {
  /* XXX: hope that compiler doesn't create a temporary */
  point_t *x = new point_t(point_t::carthesian(1,2));
  x->add(1,2);
}

The other version is to create separate named constructors. Because function overloading doesn’t work on return type I use two different names, which is ugly.

class point_t {
    int X,Y;
    point_t(int x, int y) : X(x), Y(y) { }
  public:
    /* XXX: function overloading doesn't work on return types */
    static point_t carthesian(int x, int y) { return point_t(x,y); }
    static point_t *carthesian_heap(int x, int y) { return new point_t(x,y); }
    void add(int x, int y) { X += x; Y += y; }
};

int main(int argc, char **argv) {
  point_t *x = point_t::carthesian_heap(1,2);
  x->add(1,2);
}

Is there a prettier version that is equal to the example code?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T15:54:18+00:00Added an answer on May 24, 2026 at 3:54 pm

    You can avoid named constructor idiom for this completely, and do it using an additonal dummy enum parameter to select the constructor.

    enum Carthesian {carthesian};
    enum Polar {polar};
    class point_t {
        int X,Y;
      public:
        point_t(int x, int y) : X(x), Y(y) { } // may keep as a default
        point_t(Carthesian, int x, int y) :X(x),Y(y){}
        point_t(Polar, float radius, float angle)
        : X (radius*std::cos(angle)), Y(radius*std::sin(angle)) {}
        void add(int x, int y) { X += x; Y += y; }
    };
    
    int main(int argc, char **argv) {
      point_t *x = new point_t(carthesian,1,2);
      point_t *y = new point_t(polar,0,3);
      x->add(1,2);
    }
    

    It is simple, portable, and the only overhead you will see is for the passing of the dummy enum values. In the rare case this overhead is too high for you it can be eliminated by wrapping a function call even when the construction itself is not inlined, as follows:

    enum Carthesian {carthesian};
    enum Polar {polar};
    class point_t {
        int X,Y;
        void initCarthesian(int x, int y); // may be long, not inlined
        void initPolar(float radius, float angle);
      public:
        point_t(int x, int y) : X(x), Y(y) { } // may keep as a default
        point_t(Carthesian, int x, int y)
        {initCarthesian(x,y);} // this is short and inlined
        point_t(Polar, float radius, float angle) {initPolar(radius, angle);}
        void add(int x, int y) { X += x; Y += y; }
    };
    

    Another approach is to use a derived class for construction. When using inner classes, it leads into quite a nice syntax I think:

    class point_t {
        int X,Y;
      public:
        struct carthesian;
        struct polar;
        point_t(int x, int y) : X(x), Y(y) { } // may keep as a default
        void add(int x, int y) { X += x; Y += y; }
    };
    
    struct point_t::carthesian: public point_t
    {
      carthesian(int x, int y):point_t(x,y){}
    };
    
    struct point_t::polar: public point_t
    {
      polar(float radius, float angle):point_t(radius*std::cos(angle),radius*std::sin(angle)){}
    };
    
    int main(int argc, char **argv) {
      point_t *x = new point_t::carthesian(1,2);
      point_t *y = new point_t::polar(0,3);
      x->add(1,2);
      return 0;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a struct which takes 3 named parameters in to the constructor... public
I have a string named itemIDs separated by commas(12,43,34,..) and in order to use
I have a UIWebView named wView. I want to use webViewDidFinishLoad: in my class,
Is it possible to use named arguments in a Scala constructor, and later on
How would you implement a constructor together with named functions on an object in
I'm using the Boost Parameter tutorial to create a named-parameter constructor for a playing
i have some questions about constructors in ColdFusion : must i use the name
Should I use Named Pipes, or .NET Remoting to communicate with a running process
On our Linux system we use named pipes for interprocess communication (a producer and
Iam trying to use a UserControl named CheckBoxControl as value for the ExpandControlID/CollapseControlID attributes

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.