Home > Back-end >  Why this case type conversion constructors doesn't work?
Why this case type conversion constructors doesn't work?

Time:03-28

MyInt objInt (1);
ObjInt - 3;//error, operator - not define
ObjInt=3;//right, automatic conversion for type conversion constructors
MyInt a=3;//right, automatic conversion for type conversion constructors

Could you tell me why in the case of objInt - 3 type conversion constructors doesn't work???????

CodePudding user response:

Your operator - defined not, how to define?

CodePudding user response:

May be I didn't clearly stated, directly on the code
 
#include
using namespace std;
The class MyInt
{
Private:
The int value.
Public:
MyInt (int a=4) : the value (a) {};
MyInt& Operator - (MyInt & amp; A)
{
The value -=Dr. Alue.
Return * this;
}
};

Int main ()
{
MyInt objInt (1);
ObjInt - 3;////error, operator - not define
ObjInt=3;
return 0;
}

I don't know why (objInt - 3) 3 here why don't you call type conversion but to call the constructor argument is of type int operator -? According to my thinking, if the first three are converted into MyInt type, and then call parameter is MyInt reference object operator - also makes sense,

CodePudding user response:

Why do you think that's right, wrong,
Think you should know, Myint - int indeed will be reduced to the second Myint, however, by the transformation of int Myint Myint is a what kind? His variable name is what??????
  • Related