473,750 Members | 2,209 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Specifying a defualt value for the parameters in a method

I wonder if there's a simple way to specify the default
values so that when i call a function designed to receive
an int and a double, i can specify the first but not the
latter. Something like:

int foo (int i, double d) {...}

but invoked by

int a = foo (5);

In Java that was done by simply overloading the
methods and using a bunch of similar definition, calling
each other. Is there a nicer solution?

--
Vänligen Kerstin Viltersten
(The Cool Giraffe)
May 12 '07 #1
6 1371
On 5月12æ—¥, 下åˆ3æ—¶41分 , "The Cool Giraffe" <giraf...@vilte rsten.comwrote:
I wonder if there's a simple way to specify the default
values so that when i call a function designed to receive
an int and a double, i can specify the first but not the
latter. Something like:

int foo (int i, double d) {...}

but invoked by

int a = foo (5);

In Java that was done by simply overloading the
methods and using a bunch of similar definition, calling
each other. Is there a nicer solution?

--
Vänligen Kerstin Viltersten
(The Cool Giraffe)
int foo(int i, double d=0.0)
{
// ...
}

int a = foo(5);

May 12 '07 #2
kingfox wrote/skrev/kaita/popisal/schreibt :
On 5?12?, ??3?41?, "The Cool Giraffe"
<giraf...@vilte rsten.comwrote:
>I wonder if there's a simple way to specify the default
values so that when i call a function designed to receive
an int and a double, i can specify the first but not the
latter. Something like:

int foo (int i, double d) {...}

but invoked by

int a = foo (5);

In Java that was done by simply overloading the
methods and using a bunch of similar definition, calling
each other. Is there a nicer solution?

--
Vänligen Kerstin Viltersten
(The Cool Giraffe)

int foo(int i, double d=0.0)
{
// ...
}

int a = foo(5);

Ah, that was really nice. Thanks.

However, i wonder if one should do the "double d = 0.0"
assignment in the header file, the cpp-file or perhaps
both? Which would be recommended? Which is legal?

--
Vänligen Kerstin Viltersten
(The Cool Giraffe)
May 12 '07 #3
Hi,
However, i wonder if one should do the "double d = 0.0"
assignment in the header file, the cpp-file or perhaps
both? Which would be recommended? Which is legal?
It is better to declare it in the header file so that statements that
use the default assignment in other translations units will compile.

Cheers

May 12 '07 #4
Ah, that was really nice. Thanks.
>
However, i wonder if one should do the "double d = 0.0"
assignment in the header file, the cpp-file or perhaps
both? Which would be recommended? Which is legal?
Sorry, I made a bad example. The better should be:

// in the header
int foo(int i, double d = 0.0);

// in the .cpp
int foo(int i, double d)
{
...
}

May 12 '07 #5
On May 12, 6:30 pm, "The Cool Giraffe" <giraf...@vilte rsten.com>
wrote:
kingfox wrote/skrev/kaita/popisal/schreibt :
On 5?12?, ??3?41?, "The Cool Giraffe"
<giraf...@vilte rsten.comwrote:
I wonder if there's a simple way to specify the default
values so that when i call a function designed to receive
an int and a double, i can specify the first but not the
latter. Something like:
int foo (int i, double d) {...}
but invoked by
int a = foo (5);
In Java that was done by simply overloading the
methods and using a bunch of similar definition, calling
each other. Is there a nicer solution?
--
Vänligen Kerstin Viltersten
(The Cool Giraffe)
int foo(int i, double d=0.0)
{
// ...
}
int a = foo(5);

Ah, that was really nice. Thanks.

However, i wonder if one should do the "double d = 0.0"
assignment in the header file, the cpp-file or perhaps
both? Which would be recommended? Which is legal?
The only way to do it is place it in the first place the function or
method is defined. This applies to template default parameters as
well.

It makes no sense in C++ to define methods in a non-header file to be
invoked in a different file.

The default parameter affects how the functions are called, not how
the compiler generates code for the functions.

May 12 '07 #6
kingfox wrote/skrev/kaita/popisal/schreibt :
>Ah, that was really nice. Thanks.

However, i wonder if one should do the "double d = 0.0"
assignment in the header file, the cpp-file or perhaps
both? Which would be recommended? Which is legal?

Sorry, I made a bad example. The better should be:

// in the header
int foo(int i, double d = 0.0);

// in the .cpp
int foo(int i, double d)
{
...
}
Got it. Thanks to all.
--
Vänligen Kerstin Viltersten
(The Cool Giraffe)
May 12 '07 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
2038
by: Kevin Song | last post by:
could you show me how to do defualt date in JS? I'm asp guy I can do it in asp asp <% today = now() %> <input type="text" name="today" value="<%=today%>> Thanks
3
3482
by: JT | last post by:
Hi all, I have a pretty basic question... I have a method (below) which uses ExecuteXMLReader to load the output from a FOR XML Explicit stored procedure into an XML document, and then save the document to a file. It works well. Now, I am trying to add namespace support, and want my namespace to be something like "http://MapTo837P". How do I get this incorporated into the XML document? Thanks.
1
1817
by: Rafael Veronezi | last post by:
Just to fix, correct me if I am wrong... With reference types (objects), if I assign an object to another, the assignment will be the address of the object, and not a copy of it's contents right? With value types (structs), if I assign an object to another, I'll be copying it's data to the left side of the assignment, and not it's address. This is valid with method parameters too, like, if I pass an object as a parameter, i'll be...
12
4127
by: Jose Fernandez | last post by:
Hello. I'm building a web service and I get this error. NEWS.News.CoverNews(string)': not all code paths return a value This is the WebMethod public SqlDataReader CoverNews(string Sport) {
2
2023
by: jason | last post by:
Pardon my ignorance on this. The below code works, except, when I edit a record and update the two drop downs take the first entry in the dropdownlist if not selected. I'd also like the dropdown to show the current value in edit mode. I'm sure this is a common question. I've reviewed several related post and tried them out to no avial. Some of the offerings in listgroup look right on, but are not clear on where to handle the suggested...
1
2712
by: Phillip Ian | last post by:
I've got the following: <asp:ObjectDataSource ID="lu" runat="server" TypeName="cFactory" SelectMethod="GetLookup" UpdateMethod="UpdateLookup" DataObjectTypeName="cLookupItem"> <SelectParameters> <asp:QueryStringParameter Name="which" QueryStringField="lookup" /> </SelectParameters> <UpdateParameters>
0
2388
by: Mike P | last post by:
Where exactly are the updateparameters of a gridview picked up from? I have created 2 very similar gridviews and given the updateparameters the same names as in my edititemtemplates. Yet this method has worked for 1 gridview and failed for the second gridview. Here is my gridview : <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:XConnectionString %>" SelectCommand="ViewForecast"...
2
2432
by: preeti13 | last post by:
Hi guys i am here with my another probelm please help me.trying insert the value into the data base but getting the null value error .I am getting thsi error Cannot insert the value NULL into column 'EmployeeID', table 'Accomplishments.dbo.Accomplishment'; column does not allow nulls. INSERT fails. The statement has been terminated. and my code is this using System; using System.Collections;
1
1755
by: CatchSandeepVaid | last post by:
Say i have one-to-many relationship between Product and ProductNames Product ----> PID (PK) ProductNames------> PID, USECODE, STARTTIME as composite ID and ENDTIME is another normal column. Now i want to fetch product with PID='1' and USECODE='100' and STARTTIME < CURRENTTIME < ENDTIME. CURRENTTIME value will be known at runtime. How to specify this in mapping and fetch Product & ProductNames using eager fetching?
0
8836
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9575
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9394
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9338
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
9256
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...
0
8260
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6803
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...
2
2798
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2223
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.