问题描述
1. abybody可以解释为什么C ++函数不能被它的
返回类型重载?返回类型的优先级高于强制转换规则。
例如:
char input(); //可以编译为nameinput $ char $ void
int input(); //可以编译成名称输入$ int $ void
....
int i = 3 +''0''+输入(); //可以编译为:
int(3)+ int(''0'')+ input $ int $ void()
char ch = 3+' '0''+输入(); //可编译为:
char(3)+ char(''0'')+ input $ char $ void()
是否存在可以理解的障碍?这样的
重载有一些利润,例如,我们可能不会写:
char input_char();
int input_int() ;
2.仅按回报类型进行投射。我对吗?例如:
A a;
struct B {静态B& operator +(const A&,const B&); };
B b;
struct B {静态C& operator +(const B&,const C&); };
C c;
struct D {
静态D& operator +(const D&,const D&);
D(const C&);
};
D d = A + b + C; //给我
d.operator =(D :: operator +(D :: operator +(D(a)+ D(b)),D(c)));
或声明的class_name :: operator +;改变演员
d.operator =(D(C :: operator +(B :: operator +(a,b),c)));
或这是常见问题吗?
1. Can abybody explain me why C++ function can not be overloaded by its
return type? Return type can has priority higher than casting rules.
For example:
charinput();//can be compiled to name "input$char$void"
intinput();//can be compiled to name "input$int$void"
....
inti= 3+''0''+input();//can be compiled to:
int(3)+int(''0'')+input$int$void()
charch= 3+''0''+input();//can be compiled to:
char(3)+char(''0'')+input$char$void()
Are any understandable obstacles exist? There is some profit for such
overloading, for example, we would may not write:
charinput_char();
intinput_int();
2. Cast is only by return type. Am i right? For example:
A a;
struct B{ static B& operator+ (const A&, const B&); };
B b;
struct B{ static C& operator+ (const B&, const C&); };
C c;
struct D{
static D& operator+ (const D&, const D&);
D(const C&);
};
D d=a+b+c; //give me
d.operator=( D::operator+( D::operator+( D(a)+D(b) ), D(c) ) );
or declared "class_name::operator+" change cast
d.operator=( D( C::operator+ ( B::operator+(a,b), c ) ) );
Or is it FAQ question?
推荐答案
这篇关于按其返回类型,强制转换规则重载函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!