473,387 Members | 1,572 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

odd behaviour of overloaded * operator

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?

Jun 3 '06 #1
9 1572
In article <11**********************@j55g2000cwa.googlegroups .com>,
Cl*******@hotmail.com wrote:
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.

--
Bene disserere est finis logices. -- Aristotle
Jun 3 '06 #2
<Cl*******@hotmail.com> wrote in message
news:11**********************@j55g2000cwa.googlegr oups.com...
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
Jun 3 '06 #3
Cl*******@hotmail.com wrote:
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
Jun 4 '06 #4

benben wrote:
Cl*******@hotmail.com wrote:
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


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;
}

Jun 5 '06 #5
Cl*******@hotmail.com wrote:
benben wrote:
Cl*******@hotmail.com wrote:
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!

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

Jun 5 '06 #6

Jonathan Mcdougall wrote:

class C
{
public:
C operator*(double d);
};
Wrong. Not const-correct.

C C::operator*( 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.

Jun 5 '06 #7
Earl Purple wrote:
Jonathan Mcdougall wrote:

class C
{
public:
C operator*(double d);
};
Wrong. Not const-correct.


Right.
C C::operator*( double d ) const;
or one member operator* that takes one C:

class C
{
public:
// ...

C operator*(const C& c);
};
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

Jun 5 '06 #8

Jonathan Mcdougall wrote:
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.


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.

Jun 6 '06 #9

Earl Purple wrote:
Jonathan Mcdougall wrote:
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.


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.

Jun 6 '06 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: Nitin Bhardwaj | last post by:
Thanx in advance for the response... I wanna enquire ( as it is asked many a times in Interviews that i face as an Engg PostGraduate ) about the overloading capability of the C++ Language. ...
5
by: Andy Jarrell | last post by:
I'm trying to inherit from a specific class that has an overloaded operator. The problem I'm getting is that certain overloaded operators don't seem to come with the inheritance. For example: ...
36
by: Ioannis Vranos | last post by:
For the code #include <iostream> class Blah { int i; public:
1
by: masood.iqbal | last post by:
I have a few questions regarding overloaded typecast operators and copy constructors that I would like an answer for. Thanks in advance. Masood (1) In some examples that I have seen...
3
by: karthik | last post by:
The * operator behaves in 2 different ways. It is used as the value at address operator as well as the multiplication operator. Does this mean * is overloaded in c?
2
by: B. Williams | last post by:
I have an assignment for school to Overload the operators << and >and I have written the code, but I have a problem with the insertion string function. I can't get it to recognize the second of...
5
by: raylopez99 | last post by:
I need an example of a managed overloaded assignment operator for a reference class, so I can equate two classes A1 and A2, say called ARefClass, in this manner: A1=A2;. For some strange reason...
2
by: subramanian100in | last post by:
overloaded operator=() -------------------------------- overloaded assignment operator should be a non-static MEMBER function of a class. This ensures that the first operand is an lvalue. If...
1
by: kwikius | last post by:
Is the marked section in main so-called "undefined behaviour". struct value_type{ value_type (): v(0){} value_type & operator = ( value_type const & in) { v = in.v; return *this; } int v;
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.