Overloading the C++operator
Such a question. I'm overloading the operators to work with a vector that consists of two points. There are several tasks:
- Find the scalar product of vectors; (returns double)
- Find the vector product of vectors; (returns a vector)
And it turns out such a problem that the compiler does not know which of the two methods to call (although they differ in the returned parameters). And how to solve all this?
using namespace std;
class Vec3D
{
private:
double x, y, z;
public:
Vec3D(double xx = 0, double yy = 0, double zz =0);
Vec3D operator+ (const Vec3D & V);
// double operator* (const Vec3D & V); // проблема тут
friend ostream& operator<< (ostream& cout, Vec3D & V);
friend Vec3D operator* (const double c, const Vec3D & V);
friend Vec3D operator* (const Vec3D & V1,const Vec3D & V2); // и тут
};
P.S. What you need implement septenary \ binary operations via the friend function I know
3 answers
In general, the compiler cannot determine which overloaded function to call just by the return value. For example, consider the following code snippet
void f() {}
int f() { return 0; }
f();
How do I determine which of the two functions is being called?
Therefore, functions are not overloaded by the return value.
Overload this statement as follows, as shown in the demo program below.
#include <iostream>
class Vec3D
{
private:
double x, y, z;
public:
Vec3D( double x = 0, double y = 0, double z =0) : x( x ), y( y ), z( z )
{
}
friend std::ostream & operator <<( std::ostream &os, const Vec3D &v );
friend Vec3D operator *( const Vec3D &V, double d );
friend Vec3D operator *( double d, const Vec3D &v );
friend Vec3D operator *( const Vec3D &V1, const Vec3D &V2 ); // и тут
};
std::ostream & operator <<( std::ostream &os, const Vec3D &v )
{
return os << "( " << v.x << ", " << v.y << ", " << v.z << " )";
}
Vec3D operator *( const Vec3D &v, double d )
{
return Vec3D( d * v.x, d * v.y, d * v.z );
}
Vec3D operator *( double d, const Vec3D &v )
{
return v * d;
}
Vec3D operator *( const Vec3D &v1, const Vec3D &v2 )
{
return Vec3D( v1.x * v2.x, v1.y * v2.y, v1.z * v2.z );
}
int main()
{
Vec3D v1( 1, 2, 3 );
Vec3D v2( 1, 2, 3 );
std::cout << ( 2 * v1 ) * ( v2 * 3 ) << std::endl;
return 0;
}
Its output to the console
( 6, 24, 54 )
The operator cannot be overloaded by the returned type. But you could take another operator, like operator ^
, to use it for one of the kinds of product of vectors.
As a result, you can get two operators
operator *
And
operator ^
The declaration of the overloaded operator operator ^
may look similar to the declaration of the operator operator *
except for the type of the return value.
The concept of a function signature does not include a return type, so you cannot use an overload for the same parameter types, but different return types in C++.
Alas, solve the problem differently...
P.S. It is not for nothing that they are denoted differently even in mathematics :)
Here you can try and do, for example, this (but this does not mean that it is necessary to do so):
#include <cstdio>
struct v3d
{
double x, y, z;
};
struct v3d_mr // multiplication result
{
double scalar;
v3d vector;
operator double () { return this->scalar; }
operator v3d () { return this->vector; }
};
v3d_mr operator * (const v3d &a, const v3d &b)
{
return
{
a.x*b.x + a.y*b.y + a.z*b.z,
{ a.y*b.z - a.z*b.y, a.z*b.x - a.x*b.z, a.x*b.y - b.x*a.y }
};
}
int main()
{
v3d a = {1, 2, 3}, b = {4, 5, 6};
double s = a * b;
v3d v = a * b;
printf("%lf\n", s);
printf("%lf %lf %lf\n", v.x, v.y, v.z);
return 0;
}