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 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
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*******@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."
"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 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 pointers) and for objects types call
their default constructor.
Any others points i should know?
|
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 understand that this is a rule in the language, but what is the motivation
of this rule?
|
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
| |
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:
|
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 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...
|
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...
|
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...
|
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,...
| |
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...
|
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...
|
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();...
|
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...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
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...
| |