default parameter to NULL

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Gary Wessle

    default parameter to NULL


    I have a method with the last arguments having defaults like this

    void filing(long, double,
    const myType& r_mo=NULL,
    const herType& r_tr=NULL);

    is that acceptable, if not how is it done?
    when I do the above, I get errors like

    file.h:19: error: default argument for parameter of type ‘const myType&’ has type ‘int’
    file.h:20: error: default argument for parameter of type ‘const herType&’ has type ‘int’
  • Jim Langston

    #2
    Re: default parameter to NULL

    "Gary Wessle" <phddas@yahoo.c omwrote in message
    news:m34prp959a .fsf@localhost. localdomain...
    >
    I have a method with the last arguments having defaults like this
    >
    void filing(long, double,
    const myType& r_mo=NULL,
    const herType& r_tr=NULL);
    >
    is that acceptable, if not how is it done?
    when I do the above, I get errors like
    >
    file.h:19: error: default argument for parameter of type 'const myType&'
    has type 'int'
    file.h:20: error: default argument for parameter of type 'const herType&'
    has type 'int'
    A reference can't have a NULL value, one reason being becasue you can't
    check for it. You can do that with a pointer, but not a reference.


    Comment

    • Greg

      #3
      Re: default parameter to NULL

      Gary Wessle wrote:
      I have a method with the last arguments having defaults like this
      >
      void filing(long, double,
      const myType& r_mo=NULL,
      const herType& r_tr=NULL);
      >
      is that acceptable, if not how is it done?
      when I do the above, I get errors like
      No, it is not reasonable to attempt to pass NULL for any parameter
      declared a reference - a reference requires that the caller always
      supply an object (of a suitable type) in order to call the function.

      To indicate that values for myType and herType are not required in
      order to call filing(), there are a few options available. One approach
      is for filing to accept pointers instead of references for those
      values:

      void filing( long, double,
      const myType* r_mo = NULL,
      const herType* r_tr = NULL);

      A better approach would probably be to overload filing() - with one
      overload accepting the two reference parameters and another overload of
      filing() that does not:

      void filing( long, double, const myType& r_mo, const herType&);
      void filing( long, double);

      Greg

      Comment

      • zhao.kaiyong@gmail.com

        #4
        Re: default parameter to NULL


        "Greg дµÀ£º
        "
        Gary Wessle wrote:
        I have a method with the last arguments having defaults like this

        void filing(long, double,
        const myType& r_mo=NULL,
        const herType& r_tr=NULL);

        is that acceptable, if not how is it done?
        when I do the above, I get errors like
        >
        No, it is not reasonable to attempt to pass NULL for any parameter
        declared a reference - a reference requires that the caller always
        supply an object (of a suitable type) in order to call the function.
        >
        To indicate that values for myType and herType are not required in
        order to call filing(), there are a few options available. One approach
        is for filing to accept pointers instead of references for those
        values:
        >
        void filing( long, double,
        const myType* r_mo = NULL,
        const herType* r_tr = NULL);
        >
        A better approach would probably be to overload filing() - with one
        overload accepting the two reference parameters and another overload of
        filing() that does not:
        >
        void filing( long, double, const myType& r_mo, const herType&);
        void filing( long, double);

        Greg
        I agree with you.

        Comment

        Working...