Default arguments in constructor

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

    Default arguments in constructor

    Is it permissable to place default arguments in the definition of a
    constructor rather than in the declaration.
    Following extract:

    class X
    {
    public:
    X(int arg1, int arg2);
    ....
    }

    X::X(int arg1=7, int arg2=9)
    {
    ...
    }

    compiles using Dev-C++ (mingw); fails using VC++.
    Which compiler is right?
    --
    Gary


  • Victor Bazarov

    #2
    Re: Default arguments in constructor

    "Gary Labowitz" <glabowitz@comc ast.net> wrote...[color=blue]
    > Is it permissable to place default arguments in the definition of a
    > constructor rather than in the declaration.[/color]

    Yes, but it's rather useless, don't you think? The whole
    idea to have default argument values in the declaration is
    to let compiler use them. If you stuff the default values
    in the definition (which should be in a separate module,
    I suppose), the compiler will not see them.

    If your definition right here, in the same unit as the class
    definition with the constructor declaration, then the default
    values should be picked up, since the definition is itself
    a declaration, and default values are allowed to be added in
    consecutive declarations of the same function.
    [color=blue]
    > Following extract:
    >
    > class X
    > {
    > public:
    > X(int arg1, int arg2);
    > ...
    > }
    >
    > X::X(int arg1=7, int arg2=9)
    > {
    > ...
    > }
    >
    > compiles using Dev-C++ (mingw); fails using VC++.
    > Which compiler is right?[/color]

    Neither. The code in the presented form is not compilable.
    But if you write a compilable example:

    struct A
    {
    A(int a);
    };

    A::A(int a = 10)
    {
    }

    int main()
    {
    A a;
    }

    VC++ fails _incorrectly_. Intel C++, Comeau C++, also compile
    this example without a hitch. I didn't check GCC.

    Victor



    Comment

    • Thomas Matthews

      #3
      Re: Default arguments in constructor

      Gary Labowitz wrote:
      [color=blue]
      > Is it permissable to place default arguments in the definition of a
      > constructor rather than in the declaration.
      > Following extract:
      >
      > class X
      > {
      > public:
      > X(int arg1, int arg2);
      > ...
      > }
      >
      > X::X(int arg1=7, int arg2=9)
      > {
      > ...
      > }
      >
      > compiles using Dev-C++ (mingw); fails using VC++.
      > Which compiler is right?
      > --
      > Gary
      >
      >[/color]
      The above snippet, less the "...", is valid.
      My Borland Builder compiler complains about "data in
      header: cannot precompile" when I specify default
      values in the declaration, so I used the above method.

      --
      Thomas Matthews

      C++ newsgroup welcome message:

      C++ Faq: http://www.parashift.com/c++-faq-lite
      C Faq: http://www.eskimo.com/~scs/c-faq/top.html
      alt.comp.lang.l earn.c-c++ faq:

      Other sites:
      http://www.josuttis.com -- C++ STL Library book

      Comment

      • Gianni Mariani

        #4
        Re: Default arguments in constructor

        Victor Bazarov wrote:
        ....[color=blue]
        >
        > Neither. The code in the presented form is not compilable.
        > But if you write a compilable example:
        >
        > struct A
        > {
        > A(int a);
        > };
        >
        > A::A(int a = 10)
        > {
        > }
        >
        > int main()
        > {
        > A a;
        > }
        >
        > VC++ fails _incorrectly_. Intel C++, Comeau C++, also compile
        > this example without a hitch. I didn't check GCC.[/color]

        GCC (3.3.1) eats this for breakfast. (no errors).

        Comment

        • David Rubin

          #5
          Re: Default arguments in constructor

          Victor Bazarov wrote:[color=blue]
          > "Gary Labowitz" <glabowitz@comc ast.net> wrote...
          >[color=green]
          >>Is it permissable to place default arguments in the definition of a
          >>constructor rather than in the declaration.[/color]
          >
          >
          > Yes, but it's rather useless, don't you think? The whole
          > idea to have default argument values in the declaration is
          > to let compiler use them. If you stuff the default values
          > in the definition (which should be in a separate module,
          > I suppose), the compiler will not see them.[/color]

          Moreover, other programmers usually do not (or cannot) read
          implementation files. Therefore, putting default values in the function
          declaration (.h files) is the only reasonable choice.

          /david

          --
          "As a scientist, Throckmorton knew that if he were ever to break wind in
          the echo chamber, he would never hear the end of it."

          Comment

          • Gary Labowitz

            #6
            Re: Default arguments in constructor

            "Thomas Matthews" <Thomas_Matthew sHatesSpam@sbcg lobal.net> wrote in message
            news:wmMub.3114 5$Yq7.23096@new ssvr31.news.pro digy.com...[color=blue]
            > Gary Labowitz wrote:
            >[color=green]
            > > Is it permissable to place default arguments in the definition of a
            > > constructor rather than in the declaration.
            > > Following extract:
            > >
            > > class X
            > > {
            > > public:
            > > X(int arg1, int arg2);
            > > ...
            > > }
            > >
            > > X::X(int arg1=7, int arg2=9)
            > > {
            > > ...
            > > }
            > >
            > > compiles using Dev-C++ (mingw); fails using VC++.
            > > Which compiler is right?[/color][/color]

            Summary: I apologize for not posting compilible example; I shouldn't have
            put in the elipses.
            Putting default values in the header of constructor is apparently allowed,
            but as nonsensical as using
            X::X(int arg1, int arg2):arg1(7), arg2(9){}
            except that a constructor call with arguments will use the supplied values
            rather than the defaults.
            However, standard compilers will accept it, except for Borland Builder and
            VC++ (of major compilers checked).
            Odd to see Borland on the fail list, but no surprise with VC++.
            Thanks to all who responded.
            --
            Gary


            Comment

            Working...