-2

i'm just playing with the + operator & i cannot figure out how to declare it and use it 'explicitly' please help the code is below :

class compex{

    int real;
    int img;

public:
    compex();
    compex(int,int);
    compex& explicit operator + (const compex& P1)
    friend ostream& operator <<(ostream& out,const compex& R);
};

and the implementation of the operator is:

  compex& compex :: operator + (const compex& P1)
 {
    this->real += P1.real;
   this->img += P1.img;
   return *this;
 }
asheeshr
  • 4,088
  • 6
  • 31
  • 50
David Faizulaev
  • 4,651
  • 21
  • 74
  • 124

4 Answers4

2

You cannot make (these) operators explicit (only conversion operators can be made explicit in C++11). And you don't need to. Simply avoid explicit conversions to your type, by:

  • not defining conversion operators for other types, and..
  • marking all constructors of complex that can be called with one parameter explicit.

That way, you can effectively only call operator+ with types that are already complex.

ltjax
  • 15,837
  • 3
  • 39
  • 62
  • Unfortunately, C++ also allows conversion operators as global functions and as members functions in the "from" type. Forbidding conversions to `complex` is tricky, and only trickier when trying to enforce it only for `operator +`. – Drew Dormann Mar 11 '13 at 17:46
  • It isn't just for constructors with one parameter: http://liveworkspace.org/code/30a0gl%240 – chris Mar 11 '13 at 17:52
  • 1
    @chris: good point, still missing a lot of C++11 knowledge :) I'll add that. – ltjax Mar 11 '13 at 18:17
  • @Drew: I never knew you can have conversion operators as global functions. How would you do that? – ltjax Mar 11 '13 at 18:18
1

The explicit keyword is only useful on constructors with one parameter. It will stop the compiler from using that constructor for conversions. I don't know what you are trying to accomplish by making your + operator explicit. :)

Sarien
  • 6,647
  • 6
  • 35
  • 55
  • 1
    Minor detail: explicit can also be used on constructors that have more than one parameter if the others have default values, i.e. when it can be called as if it had only one parameter. – ltjax Mar 11 '13 at 17:42
  • 2
    Also, conversion operators can be made explicit in addition to conversion constructors. – Mooing Duck Mar 11 '13 at 17:44
  • @MooingDuck - yes, but only with C++11. In C++03 conversion operators could not be marked explicit. – Pete Becker Mar 11 '13 at 19:13
0

If you want an explicit converting function, you will have to write one just for that purpose (see here) (but it will only work with one parameter).

As for your operator+(...), just remove explicit and it should work.

Compex c1(1,2);
Compex c2(3,12);
Compex c3 = c1 + c2;
Community
  • 1
  • 1
Cyrille
  • 13,905
  • 2
  • 22
  • 41
0

If you want to prevent types from implicitly converting to compex when using operator +, you can take advantage of template parameters.

Template parameters are not directly subject to type conversion rules.

class compex{
    template<class C, 
             typename std::enable_if<std::is_same<C,complex>::value>::type >  
    compex& operator + (const C& P1)
    {
       // Your code
    }
};
Drew Dormann
  • 59,987
  • 13
  • 123
  • 180