uncomfortable syntax

This is a discussion on uncomfortable syntax within the C++ Programming forums, part of the General Programming Boards category; hi, cin is a iostream object . so i could use a member function for acessing the object. i.e it ...

  1. #1
    Registered User
    Join Date
    Dec 2003
    Posts
    92

    uncomfortable syntax

    hi, cin is a iostream object . so i could use a member function for acessing the object.

    i.e it is valid to write cin.get() // as get() is a member function



    but how this is valid if i write....

    int x;
    cin>>x // surely " >>" is not a member function. how can i use this way ? is not it violating the acesses rule ? still it works..why?




    another question > how much it is important to close the file stream ? i have seen if i dont close the stream it really does not matter....no compile error.
    blue_gene

  2. #2
    I am the worst best coder Quantrizi's Avatar
    Join Date
    Mar 2002
    Posts
    644
    Quote Originally Posted by blue_gene
    int x;
    cin>>x // surely " >>" is not a member function. how can i use this way ? is not it violating the acesses rule ? still it works..why?
    the ">>" is the bitshift operator....so all you'd do, while creating the cin class/object, would be something like:
    Code:
    class cin
    {
     public:
      operator >>;
    }
    I'm not sure if that's 100% correct, but it's close to it.

    another question > how much it is important to close the file stream ? i have seen if i dont close the stream it really does not matter....no compile error.
    No compiler error because the compiler doesn't care if it's closed or not. It's very important to close it though. Memory leaks, unable to access other files, etc... will PROBABLY occur if you don't close it.

  3. #3
    Registered User
    Join Date
    Dec 2003
    Posts
    92
    your first answer is not supportive.... as i found references in many places ">>" has been usued as a operator overloading in this context.

    probabily here it is not usued as a bitshift operator.

    but still i can argue that it is not a member function.... so i can not acess that way.

    what rule is governing here?

    one more thing cin is an object , not a class ,as you have shown in your code.
    blue_gene

  4. #4
    Hardware Engineer
    Join Date
    Sep 2001
    Posts
    1,398

    Operator overloading

    In this context, >> and << are the insertion and extraction operators. They are the overloaded versions of the bitshift operators.

    That is, they have a special meaning/function which has nothing to do with bit shifting, when they are used in the iostream context.

    [EDIT] - I'm not sure if this helps, but operator overloading is really just another way of writing/calling a function.

    [EDIT 2] - Closing a file: The compiler doesn't (always) know when or if you close the file. You might have several different places in your code where the file is closed under certain conditions. Or, you code might have a bug (a logical error, not a syntax error), that causes the close-file part of your program to be skipped.
    Last edited by DougDbug; 04-09-2004 at 01:59 PM.

  5. #5
    Registered User
    Join Date
    Dec 2003
    Posts
    92
    In this context, >> and << are the insertion and extraction operators. They are the overloaded versions of the bitshift operators
    ok.....i understand the point.

    in fact, i felt it as a odd looking syntax here , as normally objects are not treated like that.

    however, well, i have to obey the syntax.

    thanks
    blue_gene

  6. #6
    vae victus! skorman00's Avatar
    Join Date
    Nov 2003
    Posts
    594
    It is understandable that you are perplexed by these operators if you are used to C. stream objects are the only operators that traditionally use them in this fashion. You could for some odd reason overload any object to use them with any other object, but that's a bad idea. Only overload these two operators if you plan on using them with streams.

    As far as the cin and cout streams, you do not have to worry about closing them. for certain streams, like the file stream for instance, you should close it once you are done using it.

    as far as it not being a member function, it's probably not. It is probably written as a free floating function.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. more then 100errors in header
    By hallo007 in forum Windows Programming
    Replies: 20
    Last Post: 05-13-2007, 08:26 AM
  2. We Got _DEBUG Errors
    By Tonto in forum Windows Programming
    Replies: 5
    Last Post: 12-22-2006, 04:45 PM
  3. Using VC Toolkit 2003
    By Noobwaker in forum Windows Programming
    Replies: 8
    Last Post: 03-13-2006, 06:33 AM
  4. Connecting to a mysql server and querying problem
    By Diod in forum C++ Programming
    Replies: 8
    Last Post: 02-13-2006, 09:33 AM
  5. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21