overload == operator for structure variables

Discussion in 'C++' started by Sreeram, Aug 4, 2004.

  1. Sreeram

    Sreeram Guest

    Hi,

    I have a class in which I have member variables for "timeb" structure. I
    want to overload the operator == for the class. I want to check whether
    2 structure variables are equal. How should I check whether 2 structure
    variables are equal?

    Please anybody could help.. I am pasting a small simple C++ code.

    #include <string>
    #include <vector>
    #include <iostream>
    #include <sys/timeb.h>

    using namespace std;
    int main()
    {
    timeb a, b;
    // some code
    vector<timeb> vecTime;
    vecTime.push_back(a);
    vecTime.push_back(b);

    if (vecTime[0] == vecTime[1])
    cout << "Equal";
    else
    cout << "Not equal";
    return(0);
    }
    Sreeram, Aug 4, 2004
    #1
    1. Advertising

  2. "Sreeram" <> wrote in message
    news:zU3Qc.30457$...
    > Hi,
    >
    > I have a class in which I have member variables for "timeb" structure. I
    > want to overload the operator == for the class. I want to check whether
    > 2 structure variables are equal. How should I check whether 2 structure
    > variables are equal?
    >
    > Please anybody could help.. I am pasting a small simple C++ code.
    >
    > #include <string>
    > #include <vector>
    > #include <iostream>
    > #include <sys/timeb.h>
    >
    > using namespace std;
    > int main()
    > {
    > timeb a, b;
    > // some code
    > vector<timeb> vecTime;
    > vecTime.push_back(a);
    > vecTime.push_back(b);
    >
    > if (vecTime[0] == vecTime[1])
    > cout << "Equal";
    > else
    > cout << "Not equal";
    > return(0);
    > }
    >


    Like this

    bool operator==(const timeb& lhs, const timeb& rhs)
    {
    // your code here
    }

    john
    John Harrison, Aug 4, 2004
    #2
    1. Advertising

  3. Sreeram

    tom_usenet Guest

    On Wed, 04 Aug 2004 17:08:53 +0530, Sreeram <>
    wrote:

    >Hi,
    >
    >I have a class in which I have member variables for "timeb" structure. I
    >want to overload the operator == for the class. I want to check whether
    >2 structure variables are equal. How should I check whether 2 structure
    >variables are equal?


    By overloading operator== for the type.

    >
    >Please anybody could help.. I am pasting a small simple C++ code.
    >
    >#include <string>
    >#include <vector>
    >#include <iostream>
    >#include <sys/timeb.h>


    inline bool operator==(timeb const& lhs, timeb const& rhs)
    {
    //do comparison, return true for equal.
    }

    Tom
    tom_usenet, Aug 4, 2004
    #3
    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. Mr Smith
    Replies:
    7
    Views:
    453
    Mr Smith
    Feb 10, 2005
  2. Matthew Monopole

    overload operator -

    Matthew Monopole, Aug 8, 2003, in forum: C++
    Replies:
    1
    Views:
    461
    Jakob Bieling
    Aug 8, 2003
  3. Jakob Bieling

    Q: Overload operator new/delete

    Jakob Bieling, Aug 8, 2003, in forum: C++
    Replies:
    0
    Views:
    385
    Jakob Bieling
    Aug 8, 2003
  4. Piotre Ugrumov
    Replies:
    3
    Views:
    359
    Nick Hounsome
    Jan 25, 2004
  5. Ying-Chieh Liao

    function overload (not operator overload)

    Ying-Chieh Liao, Oct 11, 2004, in forum: Perl Misc
    Replies:
    3
    Views:
    228
    Sherm Pendley
    Oct 11, 2004
Loading...

Share This Page