带引用变量的默认值 [英] default value with reference variable
问题描述
我想知道为什么我不能在函数参数变量中声明参考变量的默认值
?
例如。
A级
{
void f(string& str =" ;");
};
这会导致编译时错误。
Sam。
sam写道:
我想知道为什么我不能在函数参数变量中声明默认
值的引用变量?
例如。
A类
{
void f(string& str ="");
};
这会导致编译时间错误。
Sam。
您不能声明对非左值的非const引用。您要申报的字符串
与参考资料
没有相关的l值。因此......
void f(const string& str ="");
在VC ++ 6.0中运行良好
-vs_p ...
Achintya写道:
sam写道:
我想知道为什么我不能声明参考变量默认
值
在一个函数参数变量中?
例如。
类A
{f /> void f(string& str =" );
};
这导致编译时间错误。
Sam。
您不能声明对非左值的非const引用。您要申报的字符串
并没有与参考资料相关联的l值。因此......
void f(const string& str ="");
对不起,我对此有点困惑。为什么const string&作品和字符串&
不是吗?
Sam。
在VC ++ 6.0中可以正常工作
-vs_p ...
sam写道:Achintya写道:< sam写道:
我想知道为什
值
在函数参数变量中?
例如。
A类
{
void f(string& str ="");
};
这导致编译时间错误。
Sam。
您好,
您不能声明对非左值的非const引用。
"字符串"您要申报的并没有与
引用相关联的l值。因此......
void f(const string& str ="");
抱歉,我对此有点困惑。为什么const string&工作和
string&不是吗?
Sam。
将在VC ++ 6.0中正常工作
-vs_p ...
是的... C ++编译器有两种类型的''类型''与C ++相关
变量和常量。这两种类型是l值和r值。 l值
与变量相关联,r值与常数相关联。
例如:
a = 5; //没关系!但是因为''a''是一个变量,并且在内存中有一个相关的b $ b''''',因此它有l值。
5 = a; //不行,因为''5'是一个数字''const''并且没有与它相关的
a l-value但它有一个r值
.....现在在你的情况下" string是一个const。因此
无法完成。
(有关l值和r值的更多解释:
http://cplus.about.com/od/cprogrammi...def_lvalue.htm )
-vs_p ...
Hi,
I m wondering why I can''t declare reference variable for default value
in a function argument variable?
eg.
class A
{
void f(string &str="");
};
This causes compiled time error.
Sam.
sam wrote:Hi,
I m wondering why I can''t declare reference variable for default value in a function argument variable?
eg.
class A
{
void f(string &str="");
};
This causes compiled time error.
Sam.
Hi,
You cannot declare a non-const reference to non-lvalue. The "string"
that you are tring to declare doesn''t have a l-value for the reference
to get associated with. Hence...
void f(const string& str="");
will work fine in VC++ 6.0
-vs_p...
Achintya wrote:
sam wrote:Hi,
I m wondering why I can''t declare reference variable for default
valuein a function argument variable?
eg.
class A
{
void f(string &str="");
};
This causes compiled time error.
Sam.
Hi,
You cannot declare a non-const reference to non-lvalue. The "string"
that you are tring to declare doesn''t have a l-value for the reference
to get associated with. Hence...
void f(const string& str="");
Sorry, I m a bit confused with this. Why const string& works and string&
doesn''t?
Sam.
will work fine in VC++ 6.0
-vs_p...
sam wrote:Achintya wrote:sam wrote:Hi,
I m wondering why I can''t declare reference variable for default
valuein a function argument variable?
eg.
class A
{
void f(string &str="");
};
This causes compiled time error.
Sam.
Hi,
You cannot declare a non-const reference to non-lvalue. The "string" that you are tring to declare doesn''t have a l-value for the reference to get associated with. Hence...
void f(const string& str="");
Sorry, I m a bit confused with this. Why const string& works and
string& doesn''t?
Sam.will work fine in VC++ 6.0
-vs_p...
Yup...C++ compilers have two type of ''types'' associated with C++
variables and constants. The two types are l-value and r-value. l-value
is associated with variables and r-value is associated with constants.
For ex:
a = 5; // is ok! but because ''a'' is a variable and has a associated
''l''ocation in memory hence it has l-value.
5 = a; // is not OK because ''5'' is a numerical ''const'' and doesn''t have
a l-value associated with it but it has a r-value
.....and now in your situation the "" string is a const. hence that
cannot be done.
(For more explanation on l-value and r-value look:
http://cplus.about.com/od/cprogrammi...def_lvalue.htm)
-vs_p...
这篇关于带引用变量的默认值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!