operator overloading

Discussion in 'C++' started by andrew browning, Mar 30, 2006.

  1. 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;
    andrew browning, Mar 30, 2006
    #1
    1. Advertising

  2. andrew browning

    Rolf Magnus Guest

    andrew browning wrote:

    > 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.
    Rolf Magnus, Mar 30, 2006
    #2
    1. Advertising

  3. andrew browning

    Marco Costa Guest

    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.
    Marco Costa, Mar 30, 2006
    #3
  4. 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;
    andrew browning, Mar 30, 2006
    #4
  5. andrew browning

    Gavin Deane Guest

    andrew browning wrote:

    > 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
    Gavin Deane, Mar 30, 2006
    #5
  6. andrew browning

    Fei Liu Guest

    "andrew browning" <> wrote in message
    news:...
    > 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 "=="
    Fei Liu, Apr 2, 2006
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. John Smith
    Replies:
    2
    Views:
    420
    Ivan Vecerina
    Oct 6, 2004
  2. Replies:
    11
    Views:
    728
    James Kanze
    May 16, 2007
  3. hurcan solter
    Replies:
    3
    Views:
    730
    Cholo Lennon
    Aug 29, 2007
  4. Replies:
    11
    Views:
    559
  5. Replies:
    2
    Views:
    312
Loading...

Share This Page