473,494 Members | 2,027 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

default parameter value

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
12 12677
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

"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
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
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

"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


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

"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
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


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


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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

26
15514
by: Alex Panayotopoulos | last post by:
Hello all, Maybe I'm being foolish, but I just don't understand why the following code behaves as it does: - = - = - = - class listHolder: def __init__( self, myList= ): self.myList =...
2
23341
by: Vic Y | last post by:
Hi, I am trying to call a user defined function(UDF) from a stored proc, using a default parameter in the called UDF (e.g. @bid_price_type int = 0 ). However the calling stored proc complains...
2
5815
by: The Plankmeister | last post by:
Hi... I have a query which I'm accessing through PHP as a stored procedure. However, I need to be able not to pass a couple of parameters in certain situations. When I do this, I get an error: ...
1
4571
by: Marco Martin | last post by:
Hi, I've got an oledb parameter that I'm sending to an Access DB. The table I'm sending it to has a column called "Zone" of type String that has a default value of "". So I create the...
0
5259
by: silesius | last post by:
I've been using VS 2003 to develop a webapplication using C#. Today I exported the application to a remote webserver I begun experiencing problems. It's a simple application that retrieves some...
14
3241
by: cody | last post by:
I got a similar idea a couple of months ago, but now this one will require no change to the clr, is relatively easy to implement and would be a great addition to C# 3.0 :) so here we go.. To...
12
2298
by: aaragon | last post by:
Hello all. I have a simple question that seems trivial but I can't make it to work. I have a class that takes as a template argument, another class. The idea is as follows: #include...
74
15865
by: Zytan | last post by:
I have a struct constructor to initialize all of my private (or public readonly) fields. There still exists the default constructor that sets them all to zero. Is there a way to remove the...
6
1449
by: s0suk3 | last post by:
I wanted to know if there's any way to create a method that takes a default parameter, and that parameter's default value is the return value of another method of the same class. For example: ...
7
7037
by: jamesclose | last post by:
My problem is this (apologies if this is a little long ... hang in there): I can define a function in VB.NET with optional parameters that wraps a SQL procedure: Sub Test(Optional ByVal Arg1...
0
7119
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
6989
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7157
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
6873
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
5453
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
4579
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3088
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
1400
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
644
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.