+ 5

Why the default arguments of overloaded functions are not considered by the c++ come by there as part of the parameter list?

3rd Feb 2017, 7:12 AM
Srikanth Srinivasan
Srikanth Srinivasan - avatar
4 Antworten
+ 6
This is actually a good question! Lets consider what happens if this was possible (we'll overload the assignment operator): class C { public: C(int x){this->x=x;} C& operator=(const C& rhs=C(0)) //Note default argument {x = rhs.x; return *this;} private: int x; }; The usual way this works: C c1(1); C c2(2); c1 = c2; //Invokes c1.operator=(c2); c1.x is now 2 //Lets try to use default value now: c1 = ???; /*How do we write this statement to omit the rhs to allow us to call c1.operator=(); which will use the default argument?*/ In short, there is no legitimate syntax of the form c1 = ???; to allow this. If the compiler allows c1 = ; as syntax for this - then an error might pass as valid code! For example this c1 = ;c2; might be an innocent typo but will now be valid and have totally unexpected and wrong results.
3rd Feb 2017, 3:07 PM
Ettienne Gilbert
Ettienne Gilbert - avatar
- 1
that is amazing
9th Feb 2017, 6:09 PM
Pablo Avalos
Pablo Avalos - avatar
- 2
mame are u studying in tn board?
17th Feb 2017, 11:07 AM
Aravinth
Aravinth - avatar
- 3
you lucky my friends
22nd Feb 2017, 7:27 PM
rehan gameti
rehan gameti - avatar