default parameters in constructor -- design issue

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • pauldepstein@att.net

    default parameters in constructor -- design issue

    A colleague has written a counstructor of the form SomeClass(doubl e a,
    double b, double c) It then occurs to me that an indeterminate
    number of other parameters are involved so I set up a struct
    NewThingsToAdd My intention is to create a constructor
    SomeClass(doubl e a, double b, double c, NewThingsToAdd d) However, I
    don't want to replace my colleague's constructor -- merely to allow an
    enhancement. The idea that occurs to me is to define a member m of
    NewThingsToAdd such that m.prop1 = k1; m.prop2 = k2; etc (using
    public access). Then I could define the constructor SomeClass(doubl e
    a, double b, double c, NewThingsToAdd d = m) Then my colleague's
    code would still run using the particular member m. That would work
    quite nicely. However, the problem is that (as I understand it)
    default parameters can't be members of classes so the above would lead
    to illegal code. Any ideas how to implement the above.

    Many Thanks,

    Paul Epstein
  • Victor Bazarov

    #2
    Re: default parameters in constructor -- design issue

    pauldepstein@at t.net wrote:
    A colleague has written a counstructor of the form SomeClass(doubl e a,
    double b, double c) It then occurs to me that an indeterminate
    number of other parameters are involved so I set up a struct
    NewThingsToAdd My intention is to create a constructor
    SomeClass(doubl e a, double b, double c, NewThingsToAdd d) However, I
    don't want to replace my colleague's constructor -- merely to allow an
    enhancement. The idea that occurs to me is to define a member m of
    NewThingsToAdd such that m.prop1 = k1; m.prop2 = k2; etc (using
    public access). Then I could define the constructor SomeClass(doubl e
    a, double b, double c, NewThingsToAdd d = m) Then my colleague's
    code would still run using the particular member m. That would work
    quite nicely. However, the problem is that (as I understand it)
    default parameters can't be members of classes so the above would lead
    to illegal code. Any ideas how to implement the above.
    The simplest way I've seen employs a static object of the same class
    to initialise the argument. Something like

    class NewThingsToAdd {
    ...
    static NewThingsToAdd defarg;
    };

    class SomeClass {
    ...
    SomeClass(doubl e, double, double,
    NewThingsToAdd const& = NewThingsToAdd: :defarg);
    };

    Don't forget to define the static member of 'NewThingsToAdd '.

    V
    --
    Please remove capital 'A's when replying by e-mail
    I do not respond to top-posted replies, please don't ask


    Comment

    • pauldepstein@att.net

      #3
      Re: default parameters in constructor -- design issue

      On Dec 21, 12:09 pm, "Victor Bazarov" <v.Abaza...@com Acast.netwrote:
      pauldepst...@at t.net wrote:
      A colleague has written a counstructor of the form SomeClass(doubl e a,
      double b, double c)   It then occurs to me that an indeterminate
      number of other parameters are involved so I set up a struct
      NewThingsToAdd    My intention is to create a constructor
      SomeClass(doubl e a, double b, double c, NewThingsToAdd d)  However, I
      don't want to replace my colleague's constructor -- merely to allow an
      enhancement.  The idea that occurs to me is to define a member m of
      NewThingsToAdd such that m.prop1 = k1; m.prop2 = k2;   etc (using
      public access).  Then I could define the constructor SomeClass(doubl e
      a, double b, double c, NewThingsToAdd d = m)   Then my colleague's
      code would still run using the particular member m.  That would work
      quite nicely.    However,  the problem is that (as I understand it)
      default parameters can't be members of classes so the above would lead
      to illegal code.  Any ideas how to implement the above.
      >
      The simplest way I've seen employs a static object of the same class
      to initialise the argument.  Something like
      >
          class NewThingsToAdd {
              ...
              static NewThingsToAdd defarg;
          };
      >
          class SomeClass {
              ...
              SomeClass(doubl e, double, double,
                          NewThingsToAdd const& = NewThingsToAdd: :defarg);
          };
      >
      Don't forget to define the static member of 'NewThingsToAdd '.
      >
      V
      --
      Please remove capital 'A's when replying by e-mail
      I do not respond to top-posted replies, please don't ask

      Thanks, Victor. Actually, I didn't know this is legal. As a newbie,
      the only default params I've seen are of types like int or double etc.
      not user-defined class types.

      Paul Epstein

      Comment

      • jalina

        #4
        Re: default parameters in constructor -- design issue

        pauldepstein@at t.net a écrit :
        A colleague has written a counstructor of the form SomeClass(doubl e a,
        double b, double c) It then occurs to me that an indeterminate
        number of other parameters are involved so I set up a struct
        NewThingsToAdd My intention is to create a constructor
        SomeClass(doubl e a, double b, double c, NewThingsToAdd d) However, I
        don't want to replace my colleague's constructor -- merely to allow an
        enhancement. The idea that occurs to me is to define a member m of
        NewThingsToAdd such that m.prop1 = k1; m.prop2 = k2; etc (using
        public access). Then I could define the constructor SomeClass(doubl e
        a, double b, double c, NewThingsToAdd d = m)
        You could also make a second contructor with the extra parameter you need.

        Then my colleague's
        code would still run using the particular member m. That would work
        quite nicely. However, the problem is that (as I understand it)
        default parameters can't be members of classes so the above would lead
        to illegal code. Any ideas how to implement the above.
        >
        Many Thanks,
        >
        Paul Epstein

        Comment

        • pauldepstein@att.net

          #5
          Re: default parameters in constructor -- design issue

          On Dec 21, 4:24 pm, jalina <jal...@nospam. please.comwrote :
          pauldepst...@at t.net a écrit :
          >
          A colleague has written a counstructor of the form SomeClass(doubl e a,
          double b, double c)   It then occurs to me that an indeterminate
          number of other parameters are involved so I set up a struct
          NewThingsToAdd    My intention is to create a constructor
          SomeClass(doubl e a, double b, double c, NewThingsToAdd d)  However, I
          don't want to replace my colleague's constructor -- merely to allow an
          enhancement.  The idea that occurs to me is to define a member m of
          NewThingsToAdd such that m.prop1 = k1; m.prop2 = k2;   etc (using
          public access).  Then I could define the constructor SomeClass(doubl e
          a, double b, double c, NewThingsToAdd d = m)  
          >
          You could also make a second contructor with the extra parameter you need.
          >
          Then my colleague's
          >
          >
          >
          code would still run using the particular member m.  That would work
          quite nicely.    However,  the problem is that (as I understand it)
          default parameters can't be members of classes so the above would lead
          to illegal code.  Any ideas how to implement the above.
          >
          Many Thanks,
          >
          Paul Epstein- Hide quoted text -
          >
          - Show quoted text -
          I thought of that. The prob with that is that it would involve a huge
          amount of copy-pasting as much of my colleague's constructor applies
          to my own. (Not that you could have known that, of course.)

          This raises an interesting question of whether that is bad. If so
          why? Suppose a body of code contains several blocks of 200 lines or
          so which are identical. Is that something to avoid? If so why? I
          think this result is known as code-bloat.

          Paul Epstein

          Comment

          • jalina

            #6
            Re: default parameters in constructor -- design issue

            pauldepstein@at t.net a écrit :
            On Dec 21, 4:24 pm, jalina <jal...@nospam. please.comwrote :
            >pauldepst...@a tt.net a écrit :
            >>
            >>A colleague has written a counstructor of the form SomeClass(doubl e a,
            >>double b, double c) It then occurs to me that an indeterminate
            >>number of other parameters are involved so I set up a struct
            >>NewThingsToAd d My intention is to create a constructor
            >>SomeClass(dou ble a, double b, double c, NewThingsToAdd d) However, I
            >>don't want to replace my colleague's constructor -- merely to allow an
            >>enhancement . The idea that occurs to me is to define a member m of
            >>NewThingsToAd d such that m.prop1 = k1; m.prop2 = k2; etc (using
            >>public access). Then I could define the constructor SomeClass(doubl e
            >>a, double b, double c, NewThingsToAdd d = m)
            >You could also make a second contructor with the extra parameter you need.
            >>
            >Then my colleague's
            >>
            >>
            >>
            >>code would still run using the particular member m. That would work
            >>quite nicely. However, the problem is that (as I understand it)
            >>default parameters can't be members of classes so the above would lead
            >>to illegal code. Any ideas how to implement the above.
            >>Many Thanks,
            >>Paul Epstein- Hide quoted text -
            >- Show quoted text -
            >
            I thought of that. The prob with that is that it would involve a huge
            amount of copy-pasting as much of my colleague's constructor applies
            to my own. (Not that you could have known that, of course.)
            Put the common code in an init(...) function. I think the FAQ has an
            entry for that.
            >
            This raises an interesting question of whether that is bad. If so
            why? Suppose a body of code contains several blocks of 200 lines or
            so which are identical. Is that something to avoid? If so why? I
            think this result is known as code-bloat.
            >
            Paul Epstein

            Comment

            Working...