Return type for an assignment operator

Hi All,
I am wondering why return type for an assignment opearator cant be a void or int?

Cant I write assignment operator for student class like this as we do nothing with returned value?


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
Student
{
       char name[20];
       int marks;


   public:
student(char*name,int marks)
{
\\some code
}
   void operator=(student s)  //lets assume proper copy constucotrs are written
                                  for this class.

   {
        strcpy(name,s.name);
         marks=s.marks
     }
};


1
2
3
4
5
6
7
8
9
10
11
int main()
{
   student s1("venkat",95);
    student s2("Prasad,99);


s1=s2    //which also can be written as s1(s2)
          //we do nothing with this return type.so why cant it be a void or int?
      
return 0;
} 


It's to allow the chaining of results. Consider this code:
1
2
3
4
5
int a, b, c;
a = b = c = intial_int_value();

std::string d, e, f;
d = e = f = intiail_string_value();
Last edited on
@KBW

Thanks alot for the clearest and concise reply.
Topic archived. No new replies allowed.