+ 5
Why the default arguments of overloaded functions are not considered by the c++ come by there as part of the parameter list?
4 Respuestas
+ 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.
- 1
that is amazing
- 2
mame are u studying in tn board?
- 3
you lucky my friends