C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Specifying a defualt value for the parameters in a method

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language (comp.lang.c++)
View previous topic :: View next topic  
Author Message
The Cool Giraffe
Guest





PostPosted: Sat May 12, 2007 9:10 am    Post subject: Specifying a defualt value for the parameters in a method Reply with 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)
Back to top
Guest






PostPosted: Sat May 12, 2007 9:10 am    Post subject: Re: Specifying a defualt value for the parameters in a metho Reply with quote



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





PostPosted: Sat May 12, 2007 9:10 am    Post subject: Re: Specifying a defualt value for the parameters in a metho Reply with quote



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





PostPosted: Sat May 12, 2007 9:11 am    Post subject: Re: Specifying a defualt value for the parameters in a metho Reply with quote

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
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language (comp.lang.c++) All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.