473,402 Members | 2,064 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,402 software developers and data experts.

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
Jul 22 '05 #1
5 9393
"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
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
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
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
"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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

15
by: A | last post by:
Hi, A default copy constructor is created for you when you don't specify one yourself. In such case, the default copy constructor will simply do a bitwise copy for primitives (including...
11
by: The Directive | last post by:
This code will not compiled: In main function: Dot temp = new Dot( *(new Point( 5, 5 )) ); In Dot class: //Constructor with default arguments. Dot::Dot( Point& point= *(new Point( 5, 5...
12
by: Marcelo Pinto | last post by:
Hi all, In practice, what is the diference between a default constructor and an explicit default constructor? class Ai { public: Ai() {} };
6
by: Gunnar G | last post by:
If I don't define a default constructor for my class "Foo", I still get one from the compiler. But if I define a constructor that takes an argument, I don't get the default constructor, why? I...
18
by: Matt | last post by:
I try to compare the default constructor in Java and C++. In C++, a default constructor has one of the two meansings 1) a constructor has ZERO parameter Student() { //etc... } 2) a...
10
by: Ook | last post by:
I'm having trouble comprehending what exactly "default construction" is. I know how to provide a constructor with initial values, so that if I, for example, in my code do this: MyClass...
7
by: shikn | last post by:
I wrote one simple piece of codes to test const objects in C++ as below: ================================= class Empty{}; main() { const Empty e1; } ==================================
3
by: Mark | last post by:
So, I was looking at this code example by Bruce Eckel where he hides the copy constructor by making it private... but in his code I noticed that he writes NoCC n(); *with* parentheses. Now...
4
by: Jess | last post by:
Hello, I tried several books to find out the details of object initialization. Unfortunately, I'm still confused by two specific concepts, namely default-initialization and...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.