473,320 Members | 1,854 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,320 software developers and data experts.

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 1355
On 5月12æ—¥, 下åˆ3æ—¶41分, "The Cool Giraffe" <giraf...@viltersten.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...@viltersten.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...@viltersten.com>
wrote:
kingfox wrote/skrev/kaita/popisal/schreibt :
On 5?12?, ??3?41?, "The Cool Giraffe"
<giraf...@viltersten.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
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
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...
1
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?...
12
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
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...
1
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">...
0
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...
2
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...
1
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. ...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...

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.