 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
The Cool Giraffe Guest
|
Posted: Sat May 12, 2007 9:10 am Post subject: 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) |
|
| Back to top |
|
 |
Guest
|
Posted: Sat May 12, 2007 9:10 am Post subject: Re: Specifying a defualt value for the parameters in a metho |
|
|
Hi,
| Quote: | 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 |
|
| Back to top |
|
 |
kingfox Guest
|
Posted: Sat May 12, 2007 9:10 am Post subject: Re: Specifying a defualt value for the parameters in a metho |
|
|
On 5月12æ—¥, 下åˆ3æ—¶41分, "The Cool Giraffe" <giraf...@viltersten.com> wrote:
| Quote: | 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); |
|
| Back to top |
|
 |
The Cool Giraffe Guest
|
Posted: Sat May 12, 2007 9:11 am Post subject: Re: Specifying a defualt value for the parameters in a metho |
|
|
kingfox wrote/skrev/kaita/popisal/schreibt :
| Quote: | On 5?12?, ??3?41?, "The Cool Giraffe"
giraf...@viltersten.com> wrote:
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) |
|
| Back to top |
|
 |
Powered by phpBB © 2001, 2006 phpBB Group
|