Default arguments in constructor

G

Gary Labowitz

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

Victor Bazarov

Gary Labowitz said:
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
 
T

Thomas Matthews

Gary said:
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?
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
 
G

Gianni Mariani

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

David Rubin

Victor said:
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
 
G

Gary Labowitz

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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,053
Latest member
BrodieSola

Latest Threads

Top