473,657 Members | 2,566 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 9403
"Gary Labowitz" <gl*******@comc ast.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.l earn.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*******@comc ast.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************ **********@sbcg lobal.net> wrote in message
news:wm******** ***********@new ssvr31.news.pro digy.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
21186
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 pointers) and for objects types call their default constructor. Any others points i should know?
11
2603
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
4750
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
1958
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 understand that this is a rule in the language, but what is the motivation of this rule?
18
2992
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 constructor that all parameters have default values
10
2388
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 zoot(1,2); It would instantiate MyClass, passing 1 and 2 to my constructor which in turn would do whatever I want it to. Would a default constructor be as follows:
7
2567
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
2193
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 I've learnt that this does *not* actually call the default constructor (which leads me to the main question later on..) ..but then, what exactly is n? Seems like an uninitialized reference to NoCC...
4
3705
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 value-initialization. I think default-init calls default constructor for class objects and sets garbage values to PODs. Value-init also calls default constructor for class objects and sets 0s to POD types. This is what I've learned from the books (especially...
0
8392
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
1
8503
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8605
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6163
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5632
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4151
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4302
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
1953
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1611
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.