Can a class be used in default arguments?

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

    Can a class be used in default arguments?

    Can a class constructor be an argument default? It works in Borland
    C++ but not in MinGW

    The following won't compile in MinGW because of the error "Invalid
    type 'Thing1' for default"
    =============== ============
    #include <stdio.h>
    #include <stdlib.h>

    class Thing1

    {
    public:
    int mInt;
    Thing1() {mInt = 123456789;}
    };

    class Thing2
    {
    public:
    int mInt;
    public:
    void setInt(Thing1 & t = Thing1()) {mInt = t.mInt;} //PROBLEM IS
    HERE!
    };

    int main(int argc, char *argv[])
    {
    Thing2 t2;
    t2.setInt();
    printf("%d\n", t2.mInt);
    system("PAUSE") ;
    return 0;
    }
    =============== ===========
    So, is this type of default argument non-standard?

    Much Thanks,
    Adam
  • Alf P. Steinbach

    #2
    Re: Can a class be used in default arguments?

    On 3 Oct 2003 00:14:05 -0700, cruxic@softhome .net (Cruxic) wrote:
    [color=blue]
    >Can a class constructor be an argument default? It works in Borland
    >C++ but not in MinGW
    >
    >The following won't compile in MinGW because of the error "Invalid
    >type 'Thing1' for default"
    >============== =============
    >#include <stdio.h>
    >#include <stdlib.h>
    >
    >class Thing1
    >
    >{
    >public:
    > int mInt;
    > Thing1() {mInt = 123456789;}
    >};
    >
    >class Thing2
    >{
    >public:
    > int mInt;
    >public:
    > void setInt(Thing1 & t = Thing1()) {mInt = t.mInt;} //PROBLEM IS
    >HERE!
    >};
    >[/color]
    ....
    [color=blue]
    >So, is this type of default argument non-standard?[/color]

    Yes. But you can make that a reference to const object and it will
    then be standard-conforming.

    Comment

    • Kevin Goodsell

      #3
      Re: Can a class be used in default arguments?

      Cruxic wrote:
      [color=blue]
      > Can a class constructor be an argument default?[/color]

      As you stated the question, the answer is 'no'. You can't ever use a
      function as an argument (default or otherwise). You can use a function
      pointer, but you can't get a pointer to a constructor.

      But, this is not what you meant. You wanted to ask whether you can use a
      temporary object as a default argument. And you can.
      [color=blue]
      > void setInt(Thing1 & t = Thing1()) {mInt = t.mInt;} //PROBLEM IS
      > HERE![/color]

      ....however, you cannot bind a temporary to a non-const reference. That's
      the problem with this code. If the reference were const, this would be fine.

      -Kevin
      --
      My email address is valid, but changes periodically.
      To contact me please use the address from a recent posting.

      Comment

      • Siana

        #4
        Re: Can a class be used in default arguments?

        On Fri, 03 Oct 2003 17:59:38 GMT, Kevin Goodsell
        <usenet1.spamfr ee.fusion@never box.com> wrote:
        [color=blue]
        >Cruxic wrote:
        >[color=green]
        >> Can a class constructor be an argument default?[/color]
        >
        >As you stated the question, the answer is 'no'. You can't ever use a
        >function as an argument (default or otherwise). You can use a function
        >pointer, but you can't get a pointer to a constructor.
        >
        >But, this is not what you meant. You wanted to ask whether you can use a
        >temporary object as a default argument. And you can.
        >[color=green]
        >> void setInt(Thing1 & t = Thing1()) {mInt = t.mInt;} //PROBLEM IS
        >> HERE![/color]
        >
        >...however, you cannot bind a temporary to a non-const reference. That's
        >the problem with this code. If the reference were const, this would be fine.
        >
        >-Kevin[/color]

        Works fine if it's not a reference.
        void setInt(Thing1 t = Thing1())
        {
        mInt = t.mInt;
        }

        Comment

        • Kevin Goodsell

          #5
          Re: Can a class be used in default arguments?

          Siana wrote:
          [color=blue]
          > On Fri, 03 Oct 2003 17:59:38 GMT, Kevin Goodsell
          > <usenet1.spamfr ee.fusion@never box.com> wrote:
          >[color=green]
          >>
          >>...however, you cannot bind a temporary to a non-const reference. That's
          >>the problem with this code. If the reference were const, this would be fine.
          >>
          >>-Kevin[/color]
          >
          >
          > Works fine if it's not a reference.
          > void setInt(Thing1 t = Thing1())
          > {
          > mInt = t.mInt;
          > }
          >[/color]

          Assuming Thing1 has an appropriate, accessible copy constructor, then
          yes that works.

          -Kevin
          --
          My email address is valid, but changes periodically.
          To contact me please use the address from a recent posting.

          Comment

          • Cruxic

            #6
            Re: Can a class be used in default arguments?

            Great! Thanks for the help folks.

            Comment

            Working...