Default arguments in constructor

Discussion in 'C++' started by Gary Labowitz, Nov 19, 2003.

  1. 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
    Gary Labowitz, Nov 19, 2003
    #1
    1. Advertising

  2. "Gary Labowitz" <> wrote...
    > Is it permissable to place default arguments in the definition of a
    > constructor rather than in the declaration.


    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.

    > 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?


    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
    Victor Bazarov, Nov 19, 2003
    #2
    1. Advertising

  3. Gary Labowitz wrote:

    > 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
    >
    >

    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:
    http://www.slack.net/~shiva/welcome.txt
    C++ Faq: http://www.parashift.com/c -faq-lite
    C Faq: http://www.eskimo.com/~scs/c-faq/top.html
    alt.comp.lang.learn.c-c++ faq:
    http://www.raos.demon.uk/acllc-c /faq.html
    Other sites:
    http://www.josuttis.com -- C++ STL Library book
    Thomas Matthews, Nov 19, 2003
    #3
  4. Victor Bazarov wrote:
    ....
    >
    > 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.


    GCC (3.3.1) eats this for breakfast. (no errors).
    Gianni Mariani, Nov 19, 2003
    #4
  5. Gary Labowitz

    David Rubin Guest

    Victor Bazarov wrote:
    > "Gary Labowitz" <> wrote...
    >
    >>Is it permissable to place default arguments in the definition of a
    >>constructor rather than in the declaration.

    >
    >
    > 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.


    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."
    David Rubin, Nov 19, 2003
    #5
  6. "Thomas Matthews" <> wrote in message
    news:wmMub.31145$...
    > Gary Labowitz wrote:
    >
    > > 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?


    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
    Gary Labowitz, Nov 20, 2003
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Martin Magnusson
    Replies:
    4
    Views:
    527
    lilburne
    Oct 21, 2003
  2. The Directive

    Constructor & default arguments

    The Directive, Jan 14, 2004, in forum: C++
    Replies:
    11
    Views:
    908
    Peter Koch Larsen
    Jan 15, 2004
  3. Edward Diener
    Replies:
    14
    Views:
    4,851
    Josiah Carlson
    Apr 6, 2004
  4. tutmann
    Replies:
    4
    Views:
    421
  5. Generic Usenet Account
    Replies:
    10
    Views:
    2,182
Loading...

Share This Page