operator overloading

In binary operator overloading we can send more than one parameter,can one parameter be char array and another be object reference

class String
{
char s1[10];
public:
String()
{
cout<<"enter s1"<<endl;
cin>>s1;
}
};
void +operator(char name[],String s)
{
-----
}
void main()
{
char s3[10];
String s;
cout<<"enter s3"<<endl;
cin>>s3;
s+s3;//it is valid when we are overloading + operator
}
how can i call the above method how to pass 2 arguments to binary operator
i have tried it can u people help me out

Last edited on
It's "operator+", not "+operator", and it must return an object of the resulting type:

1
2
3
4
5
6
7
8
String operator+(char name[], String s) {
  return String(...);
}

// You probably also want this:
String operator+(String s, char name[]) {
  return operator+(name, s); // Implemented by calling the other version
}
Note that the declarations in ropez reply are global functions, not methods of the String class.
You would also need to declare these global functions as friends of the String class if they needed to access the private members of the class (IE the char array s1).

An alternative approach would be to first add a constructor to the String class that takes a char array as its parameter. This would then allow you to overload with String as both parameters - which would handle String + String, Char Array + String, String + Char Array (since the compiler would use the Constructor to convert the char array to a String as required).
Last edited on
Topic archived. No new replies allowed.