How do we define the operator with symmetry in relation to his parameters?



  • We need to identify the operator * for Class CardDeck.

    CardDeck& operator*(const CardDeck&, const int);
    

    Of course, the operator ' s implementation takes place through the initialization of the new Dock transferred and the cycle repeats += the required number of times.
    Is there a way to tell the compiler that the sequence of the parameters is irrelevant, that is, any sequence of the necessary parameters may be subdued to one function so as not to write the second with the reverse order, which will cause the first overturning?



  • First, the operator ' s announcement

    CardDeck& operator*(const CardDeck&, const int);
    

    Uncorrect. It is clear that there is a return of the reference to the local object of the function or removal of the skiller const the first parameter on the reference.

    In any case, this leads to an uncertain programme behaviour.

    Furthermore, there is no need to specify the qualifications const For the second parameter, since the argument is given by weight, that is, a copy of the argument.

    The operator ' s description depends on whether there is a change function from a type object int type object CardDeck or from the facility CardDeck type object int♪ Since the existence of such functions may free you from the writing of two operators or may lead to ambiguity in the choice of the operator.

    If there is no such function, you'll have to write two operators.

    Their announcements can look as follows:

    CardDeck operator *( const CardDeck &, int );
    CardDeck operator *( int, const CardDeck & );
    

    One operator can be identified through the second operator by simply changing the case. For example,

    CardDeck operator *( int value, const CardDeck &deck ) { return deck * value; } 
    

    Besides, it might also make sense for you to announce the operators. rvalue facility CardDecklike,

    CardDeck operator *( CardDeck &&, int );
    CardDeck operator *( int, CardDeck && );
    



Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2