defult参考参数 [英] defult reference parameters
问题描述
我似乎对这种代码有错误;显然c ++禁止
nonconst refs类型的默认参数。
我的替代品是什么,而不必声明为const然后
const_cast吗?
谢谢
#include< iostream>
A级{
};
B级:公共A {}
;
void f(A& a = A()){
}
int main(){
}
* puzzlecracker:我似乎得到了这种代码的错误;显然c ++禁止
nonconst refs类型的默认参数。
不,它没有。
我的替代方案是什么,而不必声明为const然后
const_cast it ?
谢谢
#include< iostream>
A班{
};
B班:公开A { }
;
格式化。
void f(A& a = A()){
>
你不能将非const引用绑定到临时引用。
-
答:因为它弄乱了人们的顺序通常阅读文字。
问:为什么这么糟糕?
A:热门帖子。
问:什么是最多的在usenet和电子邮件中烦人的事情?
puzzlecracker写道:我似乎得到了这种代码的错误;显然c ++禁止
nonconst refs类型的默认参数。
不。它将非常规引用绑定到临时禁止的内容。
我的替代方案是什么,而不必声明为const然后
const_cast呢?
为什么要const_cast呢?如果你打算改变它,你怎么能通过
temporaries?改变临时性是什么意思?
#include< iostream>
A类{
};
B班:公开A {}
;
添加:
a a_default;
void f(A& a = A()){
更改为
void f(A& a = a_default){
但是请注意,如果你更改'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''将是
a不同的默认。你可能想要将你的
参数声明为对const的引用:
void f(一个const& a = A()){
或超载''f'':
void f(){//无论何时争论
A a; //你的默认
f(a);
}
}
int main(){
}
V
> void f(A& a = A()){
您不能将非const引用绑定到临时。
我知道,谢谢。我要求一种用于实现这种行为类型的设计技术。好像我想把plymorphism和
重叠一起......
我相信Mayers对这个主题有很好的讨论。
I seem to get an error for this kind of code; apparently c++ prohibits
default params for nonconst refs types.
what are my alternatives without having to declare it const and then
const_cast it?
thanks
#include<iostream>
class A{
};
class B:public A{}
;
void f (A & a= A()){
}
int main(){
}
* puzzlecracker:I seem to get an error for this kind of code; apparently c++ prohibits
default params for nonconst refs types.
No, it doesn''t.
what are my alternatives without having to declare it const and then
const_cast it?
thanks
#include<iostream>
class A{
};
class B:public A{}
;
Formatting.
void f (A & a= A()){
You cannot bind a non-const reference to a temporary.
--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
puzzlecracker wrote:I seem to get an error for this kind of code; apparently c++ prohibits
default params for nonconst refs types.
Nope. It''s binding a non-const ref to a temporary what''s prohibited.
what are my alternatives without having to declare it const and then
const_cast it?
Why const_cast it? If you intend to change it, how can you pass
temporaries? What does it mean to change a temporary?
thanks
#include<iostream>
class A{
};
class B:public A{}
;
Add:
A a_default;
void f (A & a= A()){
Change to
void f (A & a= a_default){
But be aware that if you change ''a'' inside ''f'', you may be changing the
''a_default'', so next time it''s called with the default, it will be
a different "default". You might want to look into declaring your
argument a reference to const:
void f (A const& a = A()){
or overloading ''f'':
void f () { // no argument whatsoever
A a; // your "default"
f(a);
}
}
int main(){
}
V
> void f (A & a= A()){
You cannot bind a non-const reference to a temporary.
I know that, thanks. I am asking for a design technique used to achieve
this behaviour type. It seems as I want to couple plymorphism with
overloading....
I believe Mayers has a great discussion on this theme.
这篇关于defult参考参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!