Reputation:
I've read the section 13.5 of the working draft N3797 and I've one question. Let complex
be a class type which represents complex numbers. We can define the following operator function:
complex operator+(double d, complex c)
{
return *new complex(d+c.get_real(),c.get_imagine());
}
But how this operator function can be implements as a complex member function? Or must I to declare this operator function in every module which I've intend to use them?
Upvotes: 1
Views: 164
Reputation: 66194
What you're looking for is this:
inline complex operator +(double d, const complex& c)
{
return complex(d+c.get_real(), c.get_imagine());
}
This cannot be a member function if you want the operator to handle a left-side of the operator as a double
. It must be a free function, possibly friended if access to anything besides the public interface of complex
is needed.
If you want the right side of the add-op to be a double
, a member function can be crafted:
complex operator +(double d) const
{
return complex(d+get_real(), get_imagine());
}
Note this is assuming this definition is within the body of the complex
class definition. But in the interest of clarity I would recommend both be inline free functions.
Implicit Construction
Lined up with the usual suspects, what you're at-least-appearing to try to do is generally done with an implicit conversion constructor and a general free-function. By providing this:
complex(double d) : real(d), imagine()
{
}
in the class definition a double can implicitly construct a temporary complex
where needed. This allows this:
inline complex operator +(const complex& lhs, const complex& rhs)
{
return complex(lhs.get_real() + rhs.get_real(),
lhs.get_imagine() + rhs.get_imagine());
}
to be used as a general solution to all sensible manifestations of what you appear to want.
Upvotes: 2
Reputation: 206577
There are two ways to define a binary operator overload, such as the binary + operator, between two types.
As a member function.
When you define it as member function, the LHS of the operator is an instance of the class. The RHS of the operator is the argument to the function. That's why when you define it as member function, it can only have one argument.
As a free function.
These functions must have two arguments. The first argument is the LHS of the operator and the second argument is the RHS of the operator.
Since double
is not a class, you have to define operator+ overload between double
as LHS and complex
as RHS as a free function, with double const&
or double
as the first argument type and complex const&
or complex
as the second argument type.
Upvotes: 2
Reputation: 7625
For it to be a member, the first parameter must be of the class type(though it is not explicitly specified). But you are sending a double
as first argument. If you need this to work either make it friend
function or just non-member
function which can work only using public interface
of the Complex
class. And as others pointed out, you are leaking memory.
Upvotes: 0
Reputation: 58568
In C++, operator overloading requires class types. You cannot overload the +
operator for the basic type double
.
Also, except in certain circumstances (short-lived programs or throwaway code), the pointer resulting from a call to the new
operator should be captured, so that it can be later released with delete
, preventing a situation known as a memory leak.
Upvotes: 1