operator overloading

A

andrew browning

if i overload == and then attempt to call it in a driver program, i get
a multi-character constant error. can this operator be overloaded and
then called in such a way or is it better for me to overload = and give
it the functionality of ==? below is the code snipet:

case '==':
if (r1 == r2){
cout << true << endl;
}else{
cout << false << endl;
}
break;
 
R

Rolf Magnus

andrew said:
if i overload == and then attempt to call it in a driver program, i get
a multi-character constant error.

Sounds like you are trying to use multi-character constants, not
overloading ==.
can this operator be overloaded and then called in such a way or is it
better for me to overload = and give it the functionality of ==? below is
the code snipet:

case '==':

Here is the problem. You are using a character constant, but not giving it
signgle character, but rather two of them.
if (r1 == r2){
cout << true << endl;
}else{
cout << false << endl;
}
break;

This code doesn't contain anything connected to overloading.
 
M

Marco Costa

1. single-brace are for chars, i.e. single characters. double-braces
are for string literals. I'm guessing that's reason for the
multi-character constant error.

2. you're talking about a some different kind of operator overloading
I'm familiar with. look the topic up in google you should come up with
good reference.
 
A

andrew browning

let me clarify. i am overloading all my comparison operators (in an
implementation file) and calling them from a switch statement as
different cases of data being read in from a file. they are being
stored in a char variable. I want to include the == as a case in that
switch statement and cannot because its not a char. would it be better
to handle all the operators as string constants? below is the whole
switch:

switch (ch) {

case '*':
r3 = r1 * r2;
cout << r3 << endl;
break;
case '/':
r3 = r1 / r2;
cout << r3 << endl;
break;
case '+':
r3 = r1 + r2;
cout << r3 << endl;
break;
case '-':
r3 = r1 - r2;
cout << r3 << endl;
break;
case '==':
if ((r1==r2)){
cout << true << endl;
}else{
cout << false << endl;
}
break;
case '>':
if ( r1 > r2){
cout << true << endl;


}else{


cout << false << endl;


}


break;


case '<':


if (r1 < r2){


cout << true << endl;
 
G

Gavin Deane

andrew said:
let me clarify. i am overloading all my comparison operators (in an
implementation file) and calling them from a switch statement as
different cases of data being read in from a file. they are being
stored in a char variable. I want to include the == as a case in that
switch statement and cannot because its not a char. would it be better
to handle all the operators as string constants? below is the whole
switch:

You can only switch on integral types. char is an integral type but
std::string isn't.

You could use a string and replace the switch with if statements

if (str == "*")
{
// do your multiply test
}
else if (str == "/")
{
// do your divide test
}
else if (str == "+")
.... etc.

Or you could define an enum for the operators

enum operators { multiply_operator, divide_operator, addition_operator,
....etc. };

and switch on a value of that type.

Gavin Deane
 
F

Fei Liu

andrew browning said:
let me clarify. i am overloading all my comparison operators (in an
implementation file) and calling them from a switch statement as
different cases of data being read in from a file. they are being
stored in a char variable. I want to include the == as a case in that
switch statement and cannot because its not a char. would it be better
to handle all the operators as string constants? below is the whole
switch:

switch (ch) {

case '*':
r3 = r1 * r2;
cout << r3 << endl;
break;
case '/':
r3 = r1 / r2;
cout << r3 << endl;
break;
case '+':
r3 = r1 + r2;
cout << r3 << endl;
break;
case '-':
r3 = r1 - r2;
cout << r3 << endl;
break;
case '==':
if ((r1==r2)){
cout << true << endl;
}else{
cout << false << endl;
}
break;
use a integer value for "==", that's how getopt_long does command line
argument passing. i.e.,
case 170: .... // case "=="
 

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

Forum statistics

Threads
473,754
Messages
2,569,525
Members
44,997
Latest member
mileyka

Latest Threads

Top