Help with operator overloading!
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3

Thread: Help with operator overloading!

  1. #1
    Join Date
    Sep 2011
    Posts
    1

    Help with operator overloading!

    Hey guys/girls,

    I've got an assignment working with an encoder and a quadrature decoder/counter card - the thing I need a hand with is I'm required to overload the '!' operator such that when I write: '!MyEncoder', it resets all channels (there are 8).

    I have a class with all required functions, such as ReadChannel(int channel) and ResetChannel(int channel), I'm just not exactly sure how I'd create the overload operator member function.

    It doesn't need to return any value, it simply needs to perform a function (ie reset all channels) when '!MyEncoder' is input. I think that it requires an argument (int channel) which perhaps loops through all channels and resets them like that. I'm just confused as to how I would modify the functionality of '!'.

    Any help/guidance is MUCH appreciated

    (ps. if you'd like to see what i've done so far, i can upload the source code no problem)

  2. #2
    Join Date
    Sep 2004
    Location
    Holland (land of the dope)
    Posts
    4,123

    Re: Help with operator overloading!


  3. #3
    Join Date
    Jan 2009
    Posts
    596

    Re: Help with operator overloading!

    Sounds like yet another teacher who is abusing the language. Overloading operators should be done with a bit of thought as to whether the usage you are putting it to chimes properly with its meaning. The ! operator mean 'not', not 'reset', so is a very bad choice for this usage.

    In the real world, anyone coming across this usage, !MyEncoder, would need to check the code to see what it does. If it was simply MyEncoder.ResetAllChannels(), this would be obvious.

    There are very few uses for overloading !, the only one I can think of being for a custom built boolean class or something similar. It's probably allowed in C++ more for completeness than anything else.

    Please don't take this as a criticism of you, Absolute_N00b, I know you are just doing what your teacher tells you. But maybe mention this to them so they know better for next year!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center