earl wrote in
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.