overloading bit shift

This is a discussion on overloading bit shift within the C++ Programming forums, part of the General Programming Boards category; when overloading a bit shift in c++, does it actually take one arguments or two?? how does the function looks ...

  1. #1
    Registered User
    Join Date
    Jan 2008
    Posts
    569

    overloading bit shift

    when overloading a bit shift in c++, does it actually take one arguments or two?? how does the function looks like?

  2. #2
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    22,829
    As all other operators look like:
    lhs operator << (const lhs&, const rhs&)
    Or
    lhs operator >> (const lhs&, const rhs&)
    Remove lhs& if you're overloading inside a class.
    Last edited by Elysia; 11-28-2009 at 04:04 PM.
    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.

  3. #3
    The larch
    Join Date
    May 2006
    Posts
    3,573
    I think these operations don't modify the left-hand argument.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  4. #4
    Registered User
    Join Date
    Jan 2008
    Posts
    569
    should it be something like:

    obj operator<< (const obj& b1, const int b2)

  5. #5
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    22,829
    Ah, right. I was thinking of something else. My mistake.
    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.

  6. #6
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,745
    Quote Originally Posted by -EquinoX- View Post
    should it be something like:

    obj operator<< (const obj& b1, const int b2)
    Uh that depends dude. Explain what your bitshift operator does.

  7. #7
    Registered User
    Join Date
    Jan 2008
    Posts
    569
    shifting array elements... basically

  8. #8
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,745
    So let's say I have obj foo which is your array class. I also have bar, which is an element that I want to shift in by n places. I don't think you should use a shift operator here because it's a bit awkward.

    I mean this is going to be common right?

    foo >> n; // make a hole
    foo[n] = bar;

    Why not just make that a member function instead? It's obvious what shiftIntoPlace(bar, n); does. And you cannot possibly forget to assign something in the hole. Sorry if that is strange advice, but this is how I would write it; mainly so that I can guarantee that there is only empty space at the end of the array. Your way is a kludge to me.

  9. #9
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    22,829
    We overload operators in order to make it easy to manipulate objects in a natural way. If it makes sense to be able to shift your object in some way, go ahead and do it. We don't need any stinking members functions ala Java.
    If it doesn't make sense to do it, don't do it at all, or make a member function with a somewhat different name.
    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.

  10. #10
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,745
    Quote Originally Posted by Elysia View Post
    We overload operators in order to make it easy to manipulate objects in a natural way. If it makes sense to be able to shift your object in some way, go ahead and do it. We don't need any stinking members functions ala Java.
    If it doesn't make sense to do it, don't do it at all, or make a member function with a somewhat different name.
    Huh I see it as an operation with three operands: the object, the element, and an offset. Unless I'm forgetting one, no operator that's overloadable takes three operands. Can still do it with two, I just don't think it's very good because you have to do something like what I showed before. It's like it only did half the work.

  11. #11
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,993
    Quote Originally Posted by whiteflags
    I see it as an operation with three operands: the object, the element, and an offset.
    That is just a general purpose insertion of an element at some position anyway. The standard containers provide member function(s) named insert to do this kind of thing. I guess that if it takes one's fancy one could overload the bit shift operators to do this kind of insertion, but restricted to inserting value initialised elements at the front or the back, and this kind of fanciful operation is what I interpreted Elysia's comment to be about.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  12. #12
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    22,829
    It is the interpretation of what this should be:

    foo >> n; // make a hole
    foo[n] = bar;

    Make a hole? Do something other than shifting? Then you have to be careful about what you do. I would only accept any kind of shifting and stream operations with those operators. And when you do either shifting or stream operators, overloading operator >> to do this is perfectly acceptable.
    But if you want to create a whole or some other weird stuff, then make a member function.
    When shifting or doing stream operations, we're only operating on two objects, not three.
    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.

  13. #13
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,745
    Now it's like you changed opinions. Whatever, we agree, I guess.

  14. #14
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853
    Quote Originally Posted by whiteflags View Post
    So let's say I have obj foo which is your array class. I also have bar, which is an element that I want to shift in by n places. I don't think you should use a shift operator here because it's a bit awkward.

    I mean this is going to be common right?

    foo >> n; // make a hole
    foo[n] = bar;

    Why not just make that a member function instead? It's obvious what shiftIntoPlace(bar, n); does. And you cannot possibly forget to assign something in the hole. Sorry if that is strange advice, but this is how I would write it; mainly so that I can guarantee that there is only empty space at the end of the array. Your way is a kludge to me.
    You would shift everything by n spaces, you wouldn't create any holes. You decide if the elements that go out-of-bounds are cycled (go in front) or lost. If you had an array of strings lets say "boy" "loves "girl". Cycling you would have "girl" "boy" "loves". By losing elements you have "" "boy" "loves". Asumming left shifting of course.

    Shifting could do something similar with bitwise operators. Since this is an array you can have it not "lose" elements, but allocate one more space to save the element that would go out of bounds.

    It could be useful. Not very efficient probably, since it would be easier "shifting" the indexes rather than the elements themselves if the object is bigger than an int.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. bit operation question
    By mr_coffee in forum C Programming
    Replies: 3
    Last Post: 04-07-2009, 05:00 PM
  2. Question about Bit Manipulation.
    By parastar in forum C Programming
    Replies: 5
    Last Post: 01-28-2009, 07:46 AM
  3. 32 bit to 64 bit Ubuntu
    By Akkernight in forum Tech Board
    Replies: 15
    Last Post: 11-17-2008, 02:14 AM
  4. Array of boolean
    By DMaxJ in forum C++ Programming
    Replies: 11
    Last Post: 10-25-2001, 11:45 PM

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