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 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
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).
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."
"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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
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...
|
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...
|
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() {}
};
|
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...
|
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...
|
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...
|
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;
}
==================================
|
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...
|
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...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
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...
|
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...
|
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...
|
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,...
|
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...
| |