Specifying a defualt value for the parameters in a method

K

kingfox

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?

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

int a = foo(5);
 
T

The Cool Giraffe

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?
 
T

The Cool Giraffe

kingfox wrote/skrev/kaita/popisal/schreibt :
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?
 
T

thamizh.veriyan

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
 
K

kingfox

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)
{
...
}
 
G

Gianni Mariani

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?

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.
 
T

The Cool Giraffe

kingfox wrote/skrev/kaita/popisal/schreibt :
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top