}; 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 theoriginal meaning of operators. I read that it is not possible tochange the operator precedence by using operator overloading. Otherthan that i think that the operator functionality can be overloaded.But in the following code, the operator + has been overloaded on typeint. 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:---------------200Thanks and regards,Sarathy 解决方案 sarathy ???é????? Hi all, I just wanted to know if it is possible to overload theoriginal meaning of operators. I read that it is not possible tochange the operator precedence by using operator overloading. Otherthan that i think that the operator functionality can be overloaded.But in the following code, the operator + has been overloaded on typeint. 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 andthe other of type int. To overload the operator * for two ints, youshould 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:---------------200Thanks and regards,Sarathysarathy wrote:Hi all, I just wanted to know if it is possible to overload theoriginal meaning of operators. I read that it is not possible tochange the operator precedence by using operator overloading. Otherthan that i think that the operator functionality can be overloaded.But in the following code, the operator + has been overloaded on typeint. 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 oftype int.int c=a*b;cout << c << endl; return 0;} 这篇关于重载正常功能???的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!
10-28 19:23