By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,194 Members | 827 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,194 IT Pros & Developers. It's quick & easy.

Default arguments in constructor

P: n/a
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
Jul 22 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a
"Gary Labowitz" <gl*******@comcast.net> 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

Jul 22 '05 #2

P: n/a
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

Jul 22 '05 #3

P: n/a
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).

Jul 22 '05 #4

P: n/a
Victor Bazarov wrote:
"Gary Labowitz" <gl*******@comcast.net> 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."

Jul 22 '05 #5

P: n/a
"Thomas Matthews" <Th**********************@sbcglobal.net> wrote in message
news:wm*******************@newssvr31.news.prodigy. com...
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
Jul 22 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.