
我正在尝试一些东西
class test{ string t1="test"; testfun( string& val = this->t1 ) { /* modify val somehow */ }}; (测试:http://goo.gl/36p4CF)
目前我猜想没有技术原因,为什么这不行.
>有没有解决方法这样做,除了手动重载和设置参数?
为什么这不工作,有技术原因吗?
The keyword
thisshall not be used in a default argument of a member function.
这是一个一般问题的特殊情况:您不能在参数的默认参数中引用其他参数.即
voID f(int a,int b = a) {} 形形色色那么会是
class A{ int j;};voID f(A* this,int i = this->j) {} 这基本上是编译器将成员函数形式为voID f(int i = j){}转换成的.这源自于功能参数的评估顺序和后缀表达式(构成对象参数)的顺序是未指定的. [dcl.fct.default] / 9:
总结Default arguments are evaluated each time the function is called.
The order of evaluation of function arguments is unspecifIEd. Consequently,parameters of a function shall not be used in a default argument,even if they are not evaluated.
以上是内存溢出为你收集整理的c – 使用类成员作为成员函数的默认参数全部内容,希望文章能够帮你解决c – 使用类成员作为成员函数的默认参数所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)