By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,885 Members | 1,474 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,885 IT Pros & Developers. It's quick & easy.

default parameter value

P: n/a
class temp
{
public:
temp();
foo(char, char, char*);
private:
char matrix[150];
};

temp::foo(char p, char o, char m[150] = matrix )
{
//code
}

Is it possible to set char matrix[150] as default in the parameter list for
the mehod foo ? If so, how ?

Thanks for any help :)

Jul 19 '05 #1
Share this Question
Share on Google+
12 Replies


P: n/a
earl wrote in news:3f******@news.broadpark.no:
class temp
{
public:
temp();
foo(char, char, char*);
private:
char matrix[150];
};

temp::foo(char p, char o, char m[150] = matrix )
{
//code
}

Is it possible to set char matrix[150] as default in the parameter
list for the mehod foo ? If so, how ?


Well yes and no, this works:

void foo(char char m[150] = "moose" )
{
std::cout << m << std::endl;
}

But what is really going on is that you are passing a pointer not an
array, i.e. you are really writing:

void foo(char char *m = "moose" )
{
std::cout << m << std::endl;
}

You should really write the 2nd form as its less missleading.

Note you can also do:

void foo(char char m[3] = "moose" )
{
std::cout << m << std::endl;
}

Note the m[3] with a 6 char initializer, the compiler doesn't care
it just treated the array like it was declared a pointer.

Here's a way to do maybe what you want:

#include <iostream>

int a[3] = { 1, 2, 3 };

void foo(int (&m)[3] = a )
{
std::cerr << m[1] << "\n";
}

int main()
{
foo();
}

The above is clearer, as your asking for an array to be passed
by reference and that is what you get, not a pointer pretending
to be an array.

If you don't want to change the array (a bit like it was passed by
value) use:

void foo(int const (&m)[3] = a )
{
}

HTH

Rob.
--
http://www.victim-prime.dsl.pipex.com/
Jul 19 '05 #2

P: n/a

"Rob Williscroft" <rt*@freenet.REMOVE.co.uk> wrote in message
news:Xn**********************************@195.129. 110.130...
earl wrote in news:3f******@news.broadpark.no:
class temp
{
public:
temp();
foo(char, char, char*);
private:
char matrix[150];
};

temp::foo(char p, char o, char m[150] = matrix )
{
//code
}

Is it possible to set char matrix[150] as default in the parameter
list for the mehod foo ? If so, how ?


Well yes and no, this works:

void foo(char char m[150] = "moose" )
{
std::cout << m << std::endl;
}

But what is really going on is that you are passing a pointer not an
array, i.e. you are really writing:

void foo(char char *m = "moose" )
{
std::cout << m << std::endl;
}

You should really write the 2nd form as its less missleading.

Note you can also do:

void foo(char char m[3] = "moose" )
{
std::cout << m << std::endl;
}

Note the m[3] with a 6 char initializer, the compiler doesn't care
it just treated the array like it was declared a pointer.

Here's a way to do maybe what you want:

#include <iostream>

int a[3] = { 1, 2, 3 };

void foo(int (&m)[3] = a )
{
std::cerr << m[1] << "\n";
}

int main()
{
foo();
}

The above is clearer, as your asking for an array to be passed
by reference and that is what you get, not a pointer pretending
to be an array.

If you don't want to change the array (a bit like it was passed by
value) use:

void foo(int const (&m)[3] = a )
{
}

HTH

Rob.
--
http://www.victim-prime.dsl.pipex.com/


I tryed this code :
int main()
{
bar temp;
temp.foo();
system("PAUSE");
}

#include <iostream>
class bar
{
public:
bar();
void foo(int (&m)[5] );
private:
int matrix[5];
};
#include "bar.h"
bar::bar()
{
for(int x=0; x < 5;x++)
matrix[x] = x;
}
void bar::foo(int (&m)[5] = matrix)
{
cout << matrix[1] << endl;
}

I get an unvalid use of member bar::matrix.

What am I doing wrong ?
Jul 19 '05 #3

P: n/a
earl wrote in news:3f********@news.broadpark.no:

"Rob Williscroft" <rt*@freenet.REMOVE.co.uk> wrote in message
news:Xn**********************************@195.129. 110.130...
earl wrote in news:3f******@news.broadpark.no:
[snip]
I tryed this code :
int main()
{
bar temp;
temp.foo();
system("PAUSE");
}

#include <iostream>
class bar
{
public:
bar();
You should put the default argument in here:

void foo( int (&m)[5] = matrix );

Otherwise it will only be visible in bar.cpp or werever you define
bar::foo().
void foo(int (&m)[5] );
private:
Make this static:

static_int matrix[ 5 ];
int matrix[5];
};
#include "bar.h"
int bar::matrix[5] = { 0, 1, 2, 3, 4 };
bar::bar()
{
If you go with the static solution above you don't need this any more,
but see below if matrix must be a non-static member variable.
for(int x=0; x < 5;x++)
matrix[x] = x;
}
void bar::foo(int (&m)[5] = matrix)
{
cout << matrix[1] << endl;
cout << m[1] << endl;
}

I get an unvalid use of member bar::matrix.


The default argument can't be to a non-static member variable.

If you actually need matrix to be non-static then define 2 overloads
of bar::foo() like so:

class bar
{
public:
bar(); // init matrix
void foo( int (&m)[5] ); // no default
void foo() { foo( matrix ); }
private:
int matrix[5];
};
Rob.
--
http://www.victim-prime.dsl.pipex.com/
Jul 19 '05 #4

P: n/a
Rob Williscroft wrote:
earl wrote in news:3f******@news.broadpark.no:

Is it possible to set char matrix[150] as default in the parameter
list for the mehod foo ? If so, how ?


You should really write the 2nd form as its less missleading.


Quite right! I'm sure there is a valid reason for default
arguments, but I've never encountered one.

Jul 19 '05 #5

P: n/a

"lilburne" <li******@godzilla.net> wrote in message
news:bo*************@ID-203936.news.uni-berlin.de...
Rob Williscroft wrote:
earl wrote in news:3f******@news.broadpark.no:

[snip]

Quite right! I'm sure there is a valid reason for default
arguments, but I've never encountered one.


Avoid code duplication.

void f(int a)
{
return a*2;
}

void f(int a, int b)
{
return a*b;
}

or, using default values:
void f(int a, int b = 2)
{
return a*b;
}

of course, the example is trivial.

/dan
Jul 19 '05 #6

P: n/a


Dan Cernat wrote:

"lilburne" <li******@godzilla.net> wrote in message
news:bo*************@ID-203936.news.uni-berlin.de...
Rob Williscroft wrote:
earl wrote in news:3f******@news.broadpark.no:


[snip]

Quite right! I'm sure there is a valid reason for default
arguments, but I've never encountered one.


Avoid code duplication.

void f(int a)
{
return a*2;
}

void f(int a, int b)
{
return a*b;
}

or, using default values:
void f(int a, int b = 2)
{
return a*b;
}

of course, the example is trivial.

/dan


.... and could be solved without default arguments:

void f( int a, int b )
{
return a*b;
}

void f( int a )
{
return f( a, 2 );
}

This comes in handy, if the default arument is not constant, eg.
if the above is a member function of a class and the 'default argument'
is some class member.

--
Karl Heinz Buchegger
kb******@gascad.at
Jul 19 '05 #7

P: n/a
Karl Heinz Buchegger <kb******@gascad.at> wrote in message news:<3F***************@gascad.at>...
Dan Cernat wrote:

"lilburne" <li******@godzilla.net> wrote in message
news:bo*************@ID-203936.news.uni-berlin.de...
Rob Williscroft wrote:

> earl wrote in news:3f******@news.broadpark.no:
>
[snip]

Quite right! I'm sure there is a valid reason for default
arguments, but I've never encountered one.


Avoid code duplication.

void f(int a)
{
return a*2;
}

void f(int a, int b)
{
return a*b;
}

or, using default values:
void f(int a, int b = 2)
{
return a*b;
}

of course, the example is trivial.

/dan


... and could be solved without default arguments:

void f( int a, int b )
{
return a*b;
}

void f( int a )
{
return f( a, 2 );
}

you still have 2 functions f, even the second one is trivial. using
default arguments, reduces their number to 1 as in my example.
This comes in handy, if the default arument is not constant, Let us not confuse the readers. The default argument could be a
function call that returns the apropriate type, except your example
below.
eg.
if the above is a member function of a class and the 'default argument'
is some class member.

yes, here you are right. However, a *static* member of a class or a
static function call could be used as default arguments.

/dan
Jul 19 '05 #8

P: n/a
Dan Cernat wrote:
Karl Heinz Buchegger <kb******@gascad.at> wrote in message news:<3F***************@gascad.at>...
Dan Cernat wrote:
"lilburne" <li******@godzilla.net> wrote in message
news:bo*************@ID-203936.news.uni-berlin.de...

Rob Williscroft wrote:
>earl wrote in news:3f******@news.broadpark.no:
>

[snip]
Quite right! I'm sure there is a valid reason for default
arguments, but I've never encountered one.
Avoid code duplication.

void f(int a)
{
return a*2;
}

void f(int a, int b)
{
return a*b;
}

or, using default values:
void f(int a, int b = 2)
{
return a*b;
}

of course, the example is trivial.

/dan


... and could be solved without default arguments:

void f( int a, int b )
{
return a*b;
}

void f( int a )
{
return f( a, 2 );
}


you still have 2 functions f, even the second one is trivial. using
default arguments, reduces their number to 1 as in my example.


Not really. You still have in effect two functions f but
they share one body. You haven't reduced complexity just
disguised it.

In the calling code you have:

f(3);
f(2,6);

this looks like two different functions and anyone reading
the code will wonder what the difference is. Also anyone
calling f() needs to know whether they 'want' the default or
not, so you haven't simplified anything for them, all you
have done is saved them from typing a couple of characters.

In my experience programmers tend to work from an example.
They'll cut&paste snippets of working code, and simply
tweaking it for their current needs. They'll use the f(3)
form without realising that there is a default argument, or
whether it is appropriate for the given circumstances.

By not using default arguments you make users confront the
issues each time they use the f, not hide behind defaults.
Also you can get strange behaviour when default arguments
are used with virtual functions as the defaults aren't
inherited.

The method:

void f(int a, int b);

is still just one function, with one body, and all calls to
it are standardized.

Jul 19 '05 #9

P: n/a

"lilburne" <li******@godzilla.net> wrote in message
news:bo*************@ID-203936.news.uni-berlin.de...
Dan Cernat wrote:
Karl Heinz Buchegger <kb******@gascad.at> wrote in message news:<3F***************@gascad.at>...
Dan Cernat wrote:

"lilburne" <li******@godzilla.net> wrote in message
news:bo*************@ID-203936.news.uni-berlin.de...

>Rob Williscroft wrote:
>
>
>>earl wrote in news:3f******@news.broadpark.no:
>>

[snip]
>Quite right! I'm sure there is a valid reason for default
>arguments, but I've never encountered one.
>

Avoid code duplication.

void f(int a)
{
return a*2;
}

void f(int a, int b)
{
return a*b;
}

or, using default values:
void f(int a, int b = 2)
{
return a*b;
}

of course, the example is trivial.

/dan

... and could be solved without default arguments:

void f( int a, int b )
{
return a*b;
}

void f( int a )
{
return f( a, 2 );
}

you still have 2 functions f, even the second one is trivial. using
default arguments, reduces their number to 1 as in my example.


Not really. You still have in effect two functions f but
they share one body. You haven't reduced complexity just
disguised it.

In the calling code you have:

f(3);
f(2,6);

this looks like two different functions and anyone reading
the code will wonder what the difference is. Also anyone
calling f() needs to know whether they 'want' the default or
not, so you haven't simplified anything for them, all you
have done is saved them from typing a couple of characters.

In my experience programmers tend to work from an example.
They'll cut&paste snippets of working code, and simply
tweaking it for their current needs. They'll use the f(3)
form without realising that there is a default argument, or
whether it is appropriate for the given circumstances.

Still, in your eaxmple, they can copy and paste an example that uses f(3)
not knowing that there is another function with the same name that has two
parameters they can use. So, IMO having default values means that the
programmers should know about the default parameters, and not having default
values meant the programmers have to knou about the multiple signatures of
the functions. I guess it is sort of the same thing.

default values version:
f(int a, int b = 2); // must know about b

without default values
f(int a);
f(int a, int b); // must know about this second function too


By not using default arguments you make users confront the
issues each time they use the f, not hide behind defaults.
Also you can get strange behaviour when default arguments
are used with virtual functions as the defaults aren't
inherited.
you have a point here


The method:

void f(int a, int b);

is still just one function, with one body, and all calls to
it are standardized.

programmers will end up declaring constants for the calls that require the
default parameter.

I am wondering why the default parameters are in the standard if they are
soo bad (I'll start a new thread about this)
Use them or don't use them.

/dan
Jul 19 '05 #10

P: n/a
Dan Cernat wrote:
"lilburne" <li******@godzilla.net> wrote in message
news:bo*************@ID-203936.news.uni-berlin.de...

The method:

void f(int a, int b);

is still just one function, with one body, and all calls to
it are standardized.
programmers will end up declaring constants for the calls that require the
default parameter.


If it helps to document what is going on then sobeit.
I am wondering why the default parameters are in the standard if they are
soo bad (I'll start a new thread about this)
Use them or don't use them.


I think its a question of scale. For small programs using a
100 or so classes then default arguments might have a place.
For large systems encompassing 1000s of classes the focus is
on having things pretty upfront in the source code. The
little bits of syntactic sugar, and candy interfaces that
default arguments, and operators give, don't carry as much
weight as trying to get programmers to write code that they
really meant to write.

When default arguments creep into our programs it is usually
because someone is retrofitting a flag to the API of some
method, and they can't be bothered to fix up all the places
that the API change requires. In other words they are
hacking (perjorative sense).

Jul 19 '05 #11

P: n/a


Dan Cernat wrote:

I am wondering why the default parameters are in the standard
IMHO: to save a few keystrokes. As has been shown, it is easy to life
without them by creating a second function with a different signature.

When I dsicovered default arguments I used them a lot. After confusing
myself in the calling code I lowered their use. Today I use default
arguments in constructors only.
if they are
soo bad (I'll start a new thread about this)
Use them or don't use them.


Right. It's a personal preference.

--
Karl Heinz Buchegger
kb******@gascad.at
Jul 19 '05 #12

P: n/a


lilburne wrote:


When default arguments creep into our programs it is usually
because someone is retrofitting a flag to the API of some
method, and they can't be bothered to fix up all the places
that the API change requires. In other words they are
hacking (perjorative sense).


Even in this cases I prefer to add the missing arguments
on the callers side. The compiler will flag all code spots
where a call is done. Since it is possible to give a default
value, it is also possible to insert that very same defaut
value on the callers side. But it has the nice benefit of
looking at each call and doing a quick search in the callers
code, if the default is really apropriete. It happend more then
once that I figured out in less then 30 seconds that it was not :-)

--
Karl Heinz Buchegger
kb******@gascad.at
Jul 19 '05 #13

This discussion thread is closed

Replies have been disabled for this discussion.