Good day!

I created a structure that looks like this:

struct variables { double x,y,z; double u,v,w; vector <double> J; vector <double> Omega; variables(); }; 

which I would like to be able to multiply by a double type, i.e. Each element of the structure is multiplied by this same number (including elements of arrays).

At the moment I have implemented this using the function inside the structure, but I would like to do this by redefining the multiplication operator.

Actually, the question is how to do it? Is it necessary for this to separately implement two operators within the structure of type

 friend const variables operator*(const variables& left, const double& right); friend const variables operator*(const double& left, const variables& right); 

or can it be done somehow else? Thank you in advance!

    3 answers 3

    Yes, if you want to be able to multiply by a double type scalar both on the right and on the left, then you need to implement two versions of the overloaded operator * : one with the double parameter on the left, the other one with the double parameter on the right. (In this case, there is no need to pass double parameters using a constant link. It is more logical to simply pass by value.)

    The second operator (with the double parameter on the right) can be overloaded with both a member function and a separate function. The first (with the double parameter on the left) can only be implemented by a separate function. For this reason, for consistency, both versions of the operator are usually overloaded with a separate function, although such uniformity is not formally required.

    These individual functions do not have to be friend-functions. But if you want to work in them directly with the closed internals of a class (and usually this is necessary), then you will have to declare them as friend functions.

    And finally, no one forces you to implement these functions right inside the class. Only friend-declarations of these functions should be located inside the class, and you can arrange their implementation (definitions) outside.

    However, you currently have your class declared as a struct and all its internals are fully accessible to the outside world. In such a situation, you don’t need friend-functions at all, and there is no need to declare your operators inside the class as a friend-function.

      When you implement a friend function (operator), you actually implement an external function, it just has access to private fields. For simple structures this makes no sense, since All fields are public, you can simply implement an external function (operator).

      Unfortunately, we will have to either implement the two operators: right and left, or use library generators like boost :: operators (example) . But they may suffer performance for some cases. The proposals for the standard have automatic generation of operators, but I do not know what status it currently has.

      • What is the "fried-function"? - Abyx

      It is enough just to overload the operator inside the structure.

       const variables operator*(variables& i) { x * ix; // etc return this; } 
      • What does this have to do with the problem of multiplying by a double scalar? - AnT