重载正常功能??? [英] Overloading normal functionality ???
问题描述
大家好,
我只是想知道是否有可能超载运营商的原始含义
。我读到通过使用运算符重载来改变运算符优先级是不可能的。
其他
比我认为操作员功能可以超载。
但是在下面的代码中,操作符+已经超载了类型
int。但它似乎没有工作,因为没有调用操作符fn。
在下面的代码中,
#include< iostream>
使用命名空间std;
命名空间blackbox
{
A类>
{
public:
int a;
int operator *(int a);
};
int A :: operator *(int b)
{
返回a + b;
}
}
int main()
{
using namespace blackbox;
int a = 10;
int b = 20;
int c = a * b;
cout<< c<<结束;
返回0;
}
输出:
- -------------
谢谢和问候,
Sarathy
>
sarathy ???é?????
大家好,
我只是想知道是否有可能超载运营商的原始含义
。我读到通过使用运算符重载来改变运算符优先级是不可能的。
其他
比我认为操作员功能可以超载。
但是在下面的代码中,操作符+已经超载了类型
int。但它似乎没有工作,因为没有调用操作符fn。
在下面的代码中,
#include< iostream>
使用命名空间std;
命名空间blackbox
{
class A
{
public:
int a;
int operator *(int a);
};
int A :: operator *(int b)
{
返回a + b ;
}
您重载的运算符有两个参数,一个是A类和
其他类型为int。要使运算符*重载两个整数,你需要使用int运算符*(const int&,const int&)代替
。
>
int main()
{
使用命名空间blackbox;
int a = 10;
int b = 20;
int c = a * b;
cout<< c<<结束;
返回0;
}
输出:
- -------------
谢谢和问候,
Sarathy
sarathy写道:
大家好,
我只是想知道它是不是可能会超载运营商的原始含义。我读到通过使用运算符重载来改变运算符优先级是不可能的。
其他
比我认为操作员功能可以超载。
但是在下面的代码中,操作符+已经超载了类型
int。但它似乎没有工作,因为没有调用操作符fn。
>
在以下代码中,
>
#include< iostream>
使用命名空间std;
命名空间blackbox
{
class A
{
public:
int a;
int operator *(int a);
};
int A :: operator *(int b)
{
返回a + b;
}
}
int main()
{
使用命名空间blackbox;
int a = 10;
int b = 20;
int c = a * b;
cout<< c<<结束;
返回0;
}
输出:
- -------------
200
我不确定我完全明白你的意思之后。 :-)。
#include< iostream>
名称空间blackbox
{
class A
{
int a;
public:
A():a(0){}
A(int val):a(val){}
friend int operator *(const A& lhs,const A& rhs);
>
};
int operator *(const A& lhs,const A& rhs)
{
返回lhs.a + rhs.a;
}
}
int main()
{
使用命名空间blackbox;
A a = 10;
A b = 20;
int c = a * b;
std :: cout<< c<< std :: endl;
返回0;
}
问候,
Sumit。
#include< iostream>
命名空间blackbox
{
A级
{
公开:
A(int i):a(i){}
int a;
int operator *(int a);
};
int A :: operator *(int b )
{
返回a + b;
}
}
int main()
{
使用命名空间blackbox;
A a(10);
int b = 20;
//这是你重载的运算符,类型A之一,另一个是
类型int。
int c = a * b;
cout<< c<< endl;
返回0;
}
Hi all,
I just wanted to know if it is possible to overload the
original meaning of operators. I read that it is not possible to
change the operator precedence by using operator overloading. Other
than that i think that the operator functionality can be overloaded.
But in the following code, the operator + has been overloaded on type
int. But it does''nt seem to work as the operator fn was not invoked.
In the following code,
# include <iostream>
using namespace std;
namespace blackbox
{
class A
{
public:
int a;
int operator *(int a);
};
int A::operator * (int b)
{
return a+b;
}
}
int main()
{
using namespace blackbox;
int a=10;
int b=20;
int c=a*b;
cout << c << endl;
return 0;
}
OUTPUT:
---------------
200
Thanks and regards,
Sarathy
sarathy ???é?????
Hi all,
I just wanted to know if it is possible to overload the
original meaning of operators. I read that it is not possible to
change the operator precedence by using operator overloading. Other
than that i think that the operator functionality can be overloaded.
But in the following code, the operator + has been overloaded on type
int. But it does''nt seem to work as the operator fn was not invoked.
In the following code,
# include <iostream>
using namespace std;
namespace blackbox
{
class A
{
public:
int a;
int operator *(int a);
};
int A::operator * (int b)
{
return a+b;
}The operator you overloaded has a two parameters, one of the type A and
the other of type int. To overload the operator * for two ints, you
should use int operator * (const int &, const int &) instead.>
int main()
{
using namespace blackbox;
int a=10;
int b=20;
int c=a*b;
cout << c << endl;
return 0;
}
OUTPUT:
---------------
200
Thanks and regards,
Sarathy
sarathy wrote:Hi all,
I just wanted to know if it is possible to overload the
original meaning of operators. I read that it is not possible to
change the operator precedence by using operator overloading. Other
than that i think that the operator functionality can be overloaded.
But in the following code, the operator + has been overloaded on type
int. But it does''nt seem to work as the operator fn was not invoked.
>
In the following code,
# include <iostream>
using namespace std;
namespace blackbox
{
class A
{
public:
int a;
int operator *(int a);
};
int A::operator * (int b)
{
return a+b;
}
}
int main()
{
using namespace blackbox;
int a=10;
int b=20;
int c=a*b;
cout << c << endl;
return 0;
}
OUTPUT:
---------------
200I''m not sure I fully understand what you''re after. :-).
# include <iostream>
namespace blackbox
{
class A
{
int a;
public:
A():a(0) {}
A(int val): a(val) {}
friend int operator * (const A& lhs, const A& rhs);
};
int operator * (const A& lhs, const A& rhs)
{
return lhs.a + rhs.a;
}
}
int main()
{
using namespace blackbox;
A a = 10;
A b = 20;
int c = a * b;
std::cout << c << std::endl;
return 0;
}
Regards,
Sumit.
#include <iostream>
namespace blackbox
{
class A
{
public:
A(int i): a(i) {}
int a;
int operator *(int a);
};
int A::operator * (int b)
{
return a+b;
}
}
int main()
{
using namespace blackbox;
A a(10);
int b=20;
//This is the operator you overloaded, one of type A, the other of
type int.
int c=a*b;
cout << c << endl;
return 0;
}
这篇关于重载正常功能???的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!