Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

default parameter value

9 views
Skip to first unread message

earl

unread,
Nov 2, 2003, 5:20:23 AM11/2/03
to
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 :)

Rob Williscroft

unread,
Nov 2, 2003, 6:35:11 AM11/2/03
to
earl wrote in news:3fa4...@news.broadpark.no:

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/

earl

unread,
Nov 2, 2003, 7:38:45 AM11/2/03
to

"Rob Williscroft" <r...@freenet.REMOVE.co.uk> wrote in message
news:Xns942775B432BA1uk...@195.129.110.130...

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 ?


Rob Williscroft

unread,
Nov 2, 2003, 8:24:36 AM11/2/03
to
earl wrote in news:3fa4fb9b$1...@news.broadpark.no:

>
> "Rob Williscroft" <r...@freenet.REMOVE.co.uk> wrote in message
> news:Xns942775B432BA1uk...@195.129.110.130...
>> earl wrote in news:3fa4...@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/

lilburne

unread,
Nov 2, 2003, 9:00:21 AM11/2/03
to
Rob Williscroft wrote:

> earl wrote in news:3fa4...@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.

Dan Cernat

unread,
Nov 2, 2003, 11:14:33 PM11/2/03
to

"lilburne" <lilb...@godzilla.net> wrote in message
news:bo32ll$17eop6$1...@ID-203936.news.uni-berlin.de...

> Rob Williscroft wrote:
>
> > earl wrote in news:3fa4...@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


Karl Heinz Buchegger

unread,
Nov 3, 2003, 5:06:26 AM11/3/03
to

... 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
kbuc...@gascad.at

Dan Cernat

unread,
Nov 3, 2003, 11:30:52 AM11/3/03
to
Karl Heinz Buchegger <kbuc...@gascad.at> wrote in message news:<3FA628A2...@gascad.at>...

> Dan Cernat wrote:
> >
> > "lilburne" <lilb...@godzilla.net> wrote in message
> > news:bo32ll$17eop6$1...@ID-203936.news.uni-berlin.de...
> > > Rob Williscroft wrote:
> > >
> > > > earl wrote in news:3fa4...@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

lilburne

unread,
Nov 3, 2003, 3:15:23 PM11/3/03
to

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.

Dan Cernat

unread,
Nov 3, 2003, 6:12:12 PM11/3/03
to

"lilburne" <lilb...@godzilla.net> wrote in message
news:bo6d0p$19duil$1...@ID-203936.news.uni-berlin.de...
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


lilburne

unread,
Nov 3, 2003, 6:54:01 PM11/3/03
to
Dan Cernat wrote:

> "lilburne" <lilb...@godzilla.net> wrote in message

> news:bo6d0p$19duil$1...@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).

Karl Heinz Buchegger

unread,
Nov 4, 2003, 3:37:30 AM11/4/03
to

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

unread,
Nov 4, 2003, 3:42:05 AM11/4/03
to

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 :-)

0 new messages