|
This thread is locked; no one can reply to it. |
1
2
|
operator overloading question |
bamccaig
Member #7,536
July 2006
|
Returning a reference to a const should be a contract between the caller and object that the referenced object will persist for at least as long as the returning object itself and that the caller is not to modify it. So long as this contract is upheld then there's no harm. Even better if the returned object is immutable. I would agree that an object that exposes a keyboard state should support an interface to check the state of a key rather than returning the internal state. You might offer a way to query multiple keys at a time, but I would prefer that interface to return a copy of internal state.
-- acc.js | al4anim - Allegro 4 Animation library | Allegro 5 VS/NuGet Guide | Allegro.cc Mockup | Allegro.cc <code> Tag | Allegro 4 Timer Example (w/ Semaphores) | Allegro 5 "Winpkg" (MSVC readme) | Bambot | Blog | C++ STL Container Flowchart | Castopulence Software | Check Return Values | Derail? | Is This A Discussion? Flow Chart | Filesystem Hierarchy Standard | Clean Code Talks - Global State and Singletons | How To Use Header Files | GNU/Linux (Debian, Fedora, Gentoo) | rot (rot13, rot47, rotN) | Streaming |
Stas B.
Member #9,615
March 2008
|
Tobias Dammers said: Also note that this solution does not violate encapsulation at all - the KeyStates class adheres to the "do one thing" philosophy, and allows for a very narrow interface, providing nothing but read-only access to a snapshot of key states. By contrast, if you pass an instance class Keyboard into a function, that function can do EVERYTHING the Keyboard class exposes - read key states, change modifier states, set LED status, change keyboard layout, etc.; OTOH, if you only pass an immutable KeyStates object, the function can do nothing but read key states. That's actually a pretty sound solution. I guess I misunderstood your post. |
Tobias Dammers
Member #2,604
August 2002
|
Stas B. said: That's actually a pretty sound solution. I guess I misunderstood your post. No harm done --- |
|
1
2
|