odd behaviour of overloaded * operator

C

Cleverbum

Hi,
I've created a class and defined how it should multiply with the other
number classes, but I keep getting errors when using it. Recently I
spotted that the errors came when I used:
double * myClass
as opposed to:
myClass * double
Can someone tell me how I should be overloading my operators to avoid
the above problem?
 
J

Jim Langston

Hi,
I've created a class and defined how it should multiply with the other
number classes, but I keep getting errors when using it. Recently I
spotted that the errors came when I used:
double * myClass
as opposed to:
myClass * double
Can someone tell me how I should be overloading my operators to avoid
the above problem?

You need to define the operator* for both (const& myClass, const& double)
and (const& double, const&myClass)

Not postitive if they need to be const and & but that shoudl be easlily
figured out
 
D

Daniel T.

Hi,
I've created a class and defined how it should multiply with the other
number classes, but I keep getting errors when using it. Recently I
spotted that the errors came when I used:
double * myClass
as opposed to:
myClass * double
Can someone tell me how I should be overloading my operators to avoid
the above problem?

Make the overload an normal function instead of a member function.
 
B

benben

Hi,
I've created a class and defined how it should multiply with the other
number classes, but I keep getting errors when using it. Recently I
spotted that the errors came when I used:
double * myClass
as opposed to:
myClass * double
Can someone tell me how I should be overloading my operators to avoid
the above problem?

We don't really know! And how could we? Post some code, buddy!

Ben
 
C

Cleverbum

benben said:
We don't really know! And how could we? Post some code, buddy!

Ben

Sorry, this is how I overloaded it:

Vector operator*(double scalar)
{
Vector result;
result.x = x * scalar;
result.y = y * scalar;
result.z = z * scalar;
return result;
}
 
J

Jonathan Mcdougall

Sorry, this is how I overloaded it:

Vector operator*(double scalar)
{
Vector result;
result.x = x * scalar;
result.y = y * scalar;
result.z = z * scalar;
return result;
}

That's illegal, but I suspect this is a member function. Next time,
post complete, compilable code.

class C
{
public:
C operator*(double d);
};

void f(C& c)
{
c * 1.0;

This works because the statement becomes

c.operator*(1.0);

However, this

1.0 * c

doesn't, because 1)

1.0.operator*(c);

makes no sense and 2)

operator*(double, const C&);

doesn't exist (remember: a binary operator @ as in x@y can be applied
as a member function x.operator@(y) or as a namespace scope function
operator@(x, y)).

There are three solutions. First, define two operator* at namespace
scope (outside the class):

C operator*(double d, const C& c); // 1.0 * c
C operator*(const C& c, double d); // c * 1.0

These operators may be friends of C if they need to.

The second and third solutions only work if you can [implictly]
construct a C from a double:

class C
{
public:
C(double d);
};

In this case, either define one non member operator* that takes two Cs:

C operator*(const C& c1, const C& c2);

or one member operator* that takes one C:

class C
{
public:
// ...

C operator*(const C& c);
};

The choice between the three solution is not only a matter of taste,
but also depends on the interpretation of the concept of
"encapsulation" or "information hiding". Have fun.

Now,

1.0 * c;

creates a temporary C initialized with 1.0 and both Cs are passed to
operator*. Note that operator*'s parameters must be const references
(or by value, which is "innefficient" however), because that's the only
way you will be able to pass the temporary value (technical: this
temporary is an rvalue and cannot be bound to a non-const reference).

}


Jonathan
 
E

Earl Purple

Jonathan said:
class C
{
public:
C operator*(double d);
};

Wrong. Not const-correct.

C C::eek:perator*( double d ) const;
or one member operator* that takes one C:

class C
{
public:
// ...

C operator*(const C& c);
};

Again not const-correct and here your implicit conversion would fail
too as the temporary cannot bind to a non-const reference so it doesn't
even solve OP's problem.
 
J

Jonathan Mcdougall

Earl said:
Wrong. Not const-correct.
Right.

C C::eek:perator*( double d ) const;


Again not const-correct

Right again.
and here your implicit conversion would fail
too as the temporary cannot bind to a non-const reference so it doesn't
even solve OP's problem.

That's not quite right, though my example doesn't work as I expected.
This

void f(C& c)
{
c * 1.0;

works, but

1.0 * c;
}

doesn't, because it would require a conversion on the left side of a .
(dot):

C(1.0).operator*(c);

which is illegal.


Jonathan
 
E

Earl Purple

Jonathan said:
That's not quite right, though my example doesn't work as I expected.

I will explain why it doesn't work.

class C
{
public
C();:
C ( double d ); // explicit conversion from double
C operator*( const C& c ); // not const-correct as method should be
const
};

(Assume class C implemented).

int main()
{
double d = 1.5;
C c;
C c1 = d * c; // line that compiler flags as error
}

Compiler: no match for operator* (double, C )

because it cannot implicitly convert the double to C&. It can
impllicitly convert it to const C& but operator* as defined above takes
a LHS or C& not const C&. The compiler wont' tell you about the const -
it doesn't look that deeply. All it does is look for a match that can
work with double and no more than one implicit conversion and it
doesn't find one.
 
C

Cleverbum

Earl said:
I will explain why it doesn't work.

class C
{
public
C();:
C ( double d ); // explicit conversion from double
C operator*( const C& c ); // not const-correct as method should be
const
};

(Assume class C implemented).

int main()
{
double d = 1.5;
C c;
C c1 = d * c; // line that compiler flags as error
}

Compiler: no match for operator* (double, C )

because it cannot implicitly convert the double to C&. It can
impllicitly convert it to const C& but operator* as defined above takes
a LHS or C& not const C&. The compiler wont' tell you about the const -
it doesn't look that deeply. All it does is look for a match that can
work with double and no more than one implicit conversion and it
doesn't find one.

Perhaps I'll just remember which way round to use it. the 'complete'
code stretches to a couple of hundred MB and isn't public domain, and
the above is basically going nowhere... thanks for trying anyway.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,764
Messages
2,569,567
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top