默认参数值 [英] default parameter value
问题描述
班级温度
{
public:
temp();
foo(char,char, char *);
私人:
char matrix [150];
};
temp :: foo(char p,char o,char m [150] = matrix)
{
// code
}
是否可以在参数列表中将char矩阵[150]设置为
mehod foo的默认值?如果是这样,怎么样?
感谢您的帮助:)
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 :)
推荐答案
earl写道在新闻中:3f ****** @ news.broadpark.no:
earl wrote in news:3f******@news.broadpark.no:
class temp
{
public:
temp();
foo(char,char,char *);
私人:
char matrix [150];
};
temp :: foo(char p,char o,char m [150] = matrix)
{
// code
}
是否可以将char矩阵[150]设置为默认值在mehod foo的参数
列表中?如果是这样,怎么样?
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 ?
好的,不,这是有效的:
void foo(char char m [150] =驼鹿)
{
std :: cout<< m< std :: endl;
}
但真正发生的是你传递的指针不是
数组,即你真的在写:
void foo(char char * m =" moose")
{
std :: cout<< m< std :: endl;
}
你应该写第二个表格,因为它的误导性较小。
请注意,你也可以这样做:
void foo(char char m [3] =" moose")
{
std :: cout<< m< std :: endl;
}
注意带有6个char初始值的m [3],编译器不在乎
它只是处理了数组,就像它被声明为指针一样。
这里可以做你想做的事情:
#include< iostream>
int a [3] = {1,2,3};
void foo(int( & m)[3] = a)
{
std :: cerr<< m [1]<< " \ n";
}
int main()
{
foo ();
}
以上更清楚,因为你要求通过引用传递数组
是你得到的,而不是一个指针假装
成为一个阵列。
如果你不想改变阵列(有点像它通过
值)使用:
void foo(int const(& m)[3] = a)
{
}
HTH
Rob。
-
http://www.victim-prime.dsl.pipex.com /
" Rob Williscroft" < rt*@freenet.REMOVE.co.uk>在消息中写道
新闻:Xn ********************************** @ 195.129 。 110.130 ......
"Rob Williscroft" <rt*@freenet.REMOVE.co.uk> wrote in message
news:Xn**********************************@195.129. 110.130...
伯爵在新闻中写道:3f ****** @ news.broadpark.no:
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
}
可以在mehod foo的参数
列表中将char矩阵[150]设置为默认值吗?如果是这样,怎么样?
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 ?
是的,不,这是有效的:
void foo(char char m [150] =" moose")
{
std :: cout<< m< std :: endl;
}
但真正发生的是你传递的指针不是
数组,即你真的在写:
void foo(char char * m =" moose")
{
std :: cout<< m< std :: endl;
}
你应该写第二个表格,因为它的误导性较小。
注意你也可以这样做:
void foo(char char m [3] =" moose")
{
std :: cout<< m< std :: endl;
}
注意m [3]带有一个6 char初始化器,编译器并不在意
它只是处理了数组,就像声明它一样指针。
这可能是你想做的事情:
#include< iostream>
int a [3] = {1,2,3};
void foo(int(& m)[3] = a)
{
std :: cerr<< m [1]<< \ n;;
}
int main()
{
foo();
}
以上是更清楚的,因为你要求通过引用传递数组,这就是你得到的,而不是假装成阵列的指针。
如果你不想更改数组(有点像是通过
值传递)使用:
void foo(int const(& m)[3] = a)< HTH
Rob。
-
http://www.victim-prime.dsl.pipex.com/
我试过这段代码:
int main()
{
bar temp;
temp.foo();
system(" PAUSE");
}
#include< iostream> ;
班级酒吧
{
公开:
bar();
void foo(int(& m)[5]);
private:
int matrix [5];
};
#inclu de" bar.h"
bar :: bar()
{
for(int x = 0; x< 5; x ++)
矩阵[x] = x;
}
void bar :: foo(int(& m)[5 ] =矩阵)
{
cout<<矩阵[1]<<结束;
}
我对成员栏::矩阵的使用无效。
我是什么做错了吗?
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 ?
伯尔在新闻中写道:3f ******** @ news.broadpark.no:
earl wrote in news:3f********@news.broadpark.no:
Rob Williscroft < rt*@freenet.REMOVE.co.uk>在消息中写道
新闻:Xn ********************************** @ 195.129。 110.130 ...
"Rob Williscroft" <rt*@freenet.REMOVE.co.uk> wrote in message
news:Xn**********************************@195.129. 110.130...
earl在新闻中写道:3f ****** @ news.broadpark.no:
earl wrote in news:3f******@news.broadpark.no:
[snip]
我试过这段代码:
int main()
{
bar temp;
temp.foo();
系统(暂停);
}
#include< iostream>
班级酒吧
{
公开:
bar();
你应该把默认参数放在这里:
void foo(int(& m)[5] = matrix);
否则它只会在bar.cpp中显示或者你定义的是
bar :: foo()。
void foo (int(& m)[5]);
私人:
使这个静态:
static_int矩阵[5 ];
int matrix [5];
};
#include" bar.h"
int bar :: matrix [5] = {0,1,2,3,4};
bar :: bar()
{
如果你使用上面的静态解决方案,你不再需要这个,
但如果矩阵必须是非静态成员变量。
for(int x = 0; x< 5; x ++)
matrix [x] = x;
}
void bar :: foo(int(& m)[5] = matrix)
{
cout<<矩阵[1]<< ENDL;
cout<< m [1]<< endl;
}
我得到成员bar :: matrix的无效使用。
[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.
默认参数不能成为非静态成员变量。
如果你真的需要矩阵是非静态的,那么定义2个重载
吧: :foo()如此:
班级酒吧
{
公开:
bar( ); // init matrix
void foo(int(& m)[5]); //没有默认值
void foo(){foo(matrix); }
私人:
int matrix [5];
};
Rob。
-
http://www.victim- prime.dsl.pipex.com/
这篇关于默认参数值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!