Copy Assignment operator method calling

any one can explain how the operator overloaded methods called in C++?

Class String
{
int len;
char *str;
public:
String(const char *str1="")
{
len=strlen(str1);
str=new char[len+1];
strncpy(str,str1,len+1);
str[len]='\0';
};

String& operator = (const String &obj)
{
if(this==&obj)
return *this;
delete [] str;
len=obj.len;
str=new char [len+1];
strncpy(str,obj.str,len);
str[len]='\0';
return *this;
};
}
int main()
{
String obj1="Hello;
String obj2;
obj2=obj1;
return 0;
}
1
2
3
4
5
6
7
8
int main()
{
    String obj1="Hello"; //calls String::String(const char*) constructor
    String obj2; //calls String::String() constructor
    obj2=obj1; //calls String::operator=(const String&) function
    //It founds it. If it would fail it would try to find any other String::operator= functions
    //And convert right hand side operand to accepted type.
}
Topic archived. No new replies allowed.