Thread: Defining operators

  1. #1
    Comment your source code! Lynux-Penguin's Avatar
    Join Date
    Apr 2002
    Posts
    533

    Defining operators

    I recently learned how to declare types ex. time_t

    like
    typedef long double n_type;

    How do you create operators?
    in iostream the redefined << and >> for cout and cin (god know's why?)
    how do I make new operators.

    oh and also I am looking for a website on understanding header files because my friend is also learning and I can't explain it RIGHT. So I am looking for material in that are too.

    Any help would be appreciated.
    Asking the right question is sometimes more important than knowing the answer.
    Please read the FAQ
    C Reference Card (A MUST!)
    Pointers and Memory
    The Essentials
    CString lib

  2. #2
    Banal internet user
    Join Date
    Aug 2002
    Posts
    1,380

  3. #3
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >(god know's why?)
    Why? What if you have a class that you would like to print directly with cout?
    Code:
    #include <iostream>
    
    class Test
    {
      int i, j;
    public:
      Test ( int one, int two ) :
        i ( one ), j ( two )
      {}
    };
    
    int main()
    {
      Test t ( 10, 20 );
    
      std::cout<< t <<std::endl;
    
      return 0;
    }
    This will obviously not work because cout isn't defined for objects of class Test. You need to define it yourself:
    Code:
    #include <iostream>
    
    class Test
    {
      int i, j;
    public:
      Test ( int one, int two ) :
        i ( one ), j ( two )
      {}
      friend std::ostream& operator<< ( std::ostream& os, const Test& t )
      {
        os<< t.i <<" -- "<< t.j;
        return os;
      }
    };
    
    int main()
    {
      Test t ( 10, 20 );
    
      std::cout<< t <<std::endl;
    
      return 0;
    }
    Now everything works just peachy.

    -Prelude
    My best code is written with the delete key.

  4. #4
    pronounced 'fib' FillYourBrain's Avatar
    Join Date
    Aug 2002
    Posts
    2,297
    I think L-P was questioning the use of the shift left and shift right operators as insertion operators. That's a reasonable concern but not something that matters a whole lot. I will frequently overload the [] operators but others are more rare.
    "You are stupid! You are stupid! Oh, and don't forget, you are STUPID!" - Dexter

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Bolean Operators hurt my head. (Trouble understanding) :(
    By Funcoot in forum C++ Programming
    Replies: 3
    Last Post: 01-20-2008, 07:42 PM
  2. defining operators
    By spank in forum C++ Programming
    Replies: 6
    Last Post: 03-04-2006, 12:25 PM
  3. im extreamly new help
    By rigo305 in forum C++ Programming
    Replies: 27
    Last Post: 04-23-2004, 11:22 PM
  4. Prime Number Generator... Help !?!!
    By Halo in forum C++ Programming
    Replies: 9
    Last Post: 10-20-2003, 07:26 PM
  5. include question
    By Wanted420 in forum C++ Programming
    Replies: 8
    Last Post: 10-17-2003, 03:49 AM