Thread: operator>>

  1. #1
    Registered User
    Join Date
    May 2008
    Posts
    141

    operator>>

    What does it mean to have operator as a prefix to >>, or != or ==, or any of the other operands?

  2. #2
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    You mean things like:
    Code:
    bool operator==( const Obj& lhs, const Obj& rhs );
    It's defining a custom operator (i.e. it's a function).

  3. #3
    Registered User
    Join Date
    May 2008
    Posts
    141
    Quote Originally Posted by cpjust View Post
    You mean things like:
    Code:
    bool operator==( const Obj& lhs, const Obj& rhs );
    It's defining a custom operator (i.e. it's a function).
    Why do you need custom operators?

  4. #4
    int x = *((int *) NULL); Cactus_Hugger's Avatar
    Join Date
    Jul 2003
    Location
    Banks of the River Styx
    Posts
    902
    So that you can do things like this:
    Code:
    std::cout << "Hello World." << std::endl;
    or this:
    Code:
    std::string str = std::string("Hello") + " World.";
    Generally, when defining operators for classes, try to stay true to the original meaning of the operator. (ie, '+' means 'add')
    long time; /* know C? */
    Unprecedented performance: Nothing ever ran this slow before.
    Any sufficiently advanced bug is indistinguishable from a feature.
    Real Programmers confuse Halloween and Christmas, because dec 25 == oct 31.
    The best way to accelerate an IBM is at 9.8 m/s/s.
    recursion (re - cur' - zhun) n. 1. (see recursion)

  5. #5
    Registered User
    Join Date
    May 2008
    Posts
    141
    [QUOTE=Cactus_Hugger;775139]So that you can do things like this:
    Code:
    std::cout << "Hello World." << std::endl;
    or this:
    Code:
    std::string str = std::string("Hello") + " World.";
    So its like, you could assign them in a different way, or use operators in a different way for classes???

  6. #6
    In the Land of Diddly-Doo g4j31a5's Avatar
    Join Date
    Jul 2006
    Posts
    476
    Just like Cactus_Hugger said, you usually need to overload certain operators of an object (e.g. the copy constructor / '=', or comparisons like <, ==, or >). So, if it's not overloaded, it won't do what you want it to do.

    E.g. a class 'Matrix' to define a 3d matrix. If you haven't overloaded the 'equal to' operator and call this:
    Code:
    class Matrix
    {
       private:
          float x;
          float y;
          float z;
    ...
    }
    Matrix a, b;
    ....
    if (a == b)
    {
    ...
    }
    It would mostly returned a false because it wouldn't compare the values of the x, y, z. Now, if you do an operator overload like:
    Code:
    class Matrix
    {
       private:
          float x;
          float y;
          float z;
       public:
          bool operator=(const Matrix &operand)
          {
                return ((this->x == operand->x) &&
                            (this->y == operand->y) &&
                            (this->z == operand->z) &&);
          }
    }
    It would compare between two matrices just like you wanted it to.
    ERROR: Brain not found. Please insert a new brain!

    “Do nothing which is of no use.” - Miyamoto Musashi.

  7. #7
    Registered User
    Join Date
    May 2008
    Posts
    141
    Quote Originally Posted by g4j31a5 View Post
    Just like Cactus_Hugger said, you usually need to overload certain operators of an object (e.g. the copy constructor / '=', or comparisons like <, ==, or >). So, if it's not overloaded, it won't do what you want it to do.

    E.g. a class 'Matrix' to define a 3d matrix. If you haven't overloaded the 'equal to' operator and call this:
    Code:
    class Matrix
    {
       private:
          float x;
          float y;
          float z;
    ...
    }
    Matrix a, b;
    ....
    if (a == b)
    {
    ...
    }
    It would mostly returned a false because it wouldn't compare the values of the x, y, z. Now, if you do an operator overload like:
    Code:
    class Matrix
    {
       private:
          float x;
          float y;
          float z;
       public:
          bool operator=(const Matrix &operand)
          {
                return ((this->x == operand->x) &&
                            (this->y == operand->y) &&
                            (this->z == operand->z) &&);
          }
    }
    It would compare between two matrices just like you wanted it to.
    Cool.. But wouldn't you need bool operator==(const Matrix &operand) instead?

  8. #8
    In the Land of Diddly-Doo g4j31a5's Avatar
    Join Date
    Jul 2006
    Posts
    476
    Quote Originally Posted by bobbelPoP View Post
    Cool.. But wouldn't you need bool operator==(const Matrix &operand) instead?
    Oh right. Sorry, it's 1 hour to go before the end of the workday (yay!). So my head is kinda messed up right now.
    ERROR: Brain not found. Please insert a new brain!

    “Do nothing which is of no use.” - Miyamoto Musashi.

  9. #9
    Registered User
    Join Date
    May 2008
    Posts
    141
    Quote Originally Posted by g4j31a5 View Post
    Oh right. Sorry, it's 1 hour to go before the end of the workday (yay!). So my head is kinda messed up right now.
    Ah, well thanks anyway, but can it be anything besides &operand? I dont know what &operand is?

  10. #10
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    The type is const Matrix& (this is why I prefer to type const Matrix& something and not const Matrix &something).
    Essentially, it takes a (const) reference (the &) to an object of type Matrix.
    Const reference means it cannot modify the object.

    But it can be anything you like. So long as it makes sense.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. no match for 'operator>>'
    By Taka in forum C++ Programming
    Replies: 3
    Last Post: 03-30-2009, 12:17 AM
  2. Templated operator>>
    By Scarvenger in forum C++ Programming
    Replies: 4
    Last Post: 10-11-2007, 05:41 PM