Beautiful C++: 30 Core Guidelines for Writing Clean, Safe, and Fast Code by J. Guy Davidson & Kate Gregory
Author:J. Guy Davidson & Kate Gregory
Language: eng
Format: epub, mobi
Publisher: Addison Wesley
Published: 2022-08-15T00:00:00+00:00
Implementing a dual interface
The codebase ended up being scattered throughout with a dual interface, one of which was const and one of which was non-const. Member functions would be duplicated with const qualification, like this:
Click here to view code image
class soldier { public: commander& get_commander(); commander const& get_commander() const; };
Every soldier has a commander, but finding out who it is requires traversing quite a few objects and making a few queries. Rather than duplicating the code and the accompanying maintenance burden, it is tempting to forward to the const-qualified overload, like this:
Click here to view code image
commander& soldier::get_commander() { return const_cast<commander&>( static_cast<soldier const&>(*this).get_commander()); }
Although you are const_casting the return type, it is reasonably safe to assume that, since this is a non-const function, this is not a dangerous thing to do. It goes against the spirit of the guideline, though. Fortunately, since the introduction of trailing return types in C++11, there is a better solution:
Click here to view code image
class soldier { public: commander& get_commander(); commander const& get_commander() const; private: template <class T> static auto get_commander_impl(T& t) -> decltype(t.get_commander) { // do the work } };
The public get_commander functions simply forward to the static function template. The superior feature of this solution is that the function template knows if it is oper-ating on a soldier const object. The const qualifier is part of the type T. If the implementation breaks const, the compiler will emit an error telling you so. No cast-ing is required, and that is a good thing, since casting is ugly.
This is not always going to work, though. Consider the current_animation example:
Click here to view code image
class unit { public: animation* current_animation(); animation const* current_animation() const; private: animation* m_animation; };
You might look at this and think that the function simply returns a pointer to the animation. Sadly, it is not quite as simple as that, and there is a fair amount of logic to do with animation blending that we need to go through.
It is tempting to carry out the same trick. However, if the calling code expects to modify the animation, and the const-qualified member function does not expect the returned value, which it does not own, to be modified, we have a bug. In this case, though, it is safe to assume that the implementation of each function will be different.
Download
Beautiful C++: 30 Core Guidelines for Writing Clean, Safe, and Fast Code by J. Guy Davidson & Kate Gregory.mobi
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.
C | C++ |
Tutorials | Visual C++ |
Hello! Python by Anthony Briggs(9926)
OCA Java SE 8 Programmer I Certification Guide by Mala Gupta(9802)
The Mikado Method by Ola Ellnestam Daniel Brolund(9787)
Algorithms of the Intelligent Web by Haralambos Marmanis;Dmitry Babenko(8309)
Sass and Compass in Action by Wynn Netherland Nathan Weizenbaum Chris Eppstein Brandon Mathis(7789)
Test-Driven iOS Development with Swift 4 by Dominik Hauser(7771)
Grails in Action by Glen Smith Peter Ledbrook(7705)
The Well-Grounded Java Developer by Benjamin J. Evans Martijn Verburg(7566)
Windows APT Warfare by Sheng-Hao Ma(6929)
Layered Design for Ruby on Rails Applications by Vladimir Dementyev(6664)
Blueprints Visual Scripting for Unreal Engine 5 - Third Edition by Marcos Romero & Brenden Sewell(6525)
Secrets of the JavaScript Ninja by John Resig Bear Bibeault(6423)
Kotlin in Action by Dmitry Jemerov(5073)
Hands-On Full-Stack Web Development with GraphQL and React by Sebastian Grebe(4323)
Solidity Programming Essentials by Ritesh Modi(4054)
Functional Programming in JavaScript by Mantyla Dan(4044)
WordPress Plugin Development Cookbook by Yannick Lefebvre(3845)
Unity 3D Game Development by Anthony Davis & Travis Baptiste & Russell Craig & Ryan Stunkel(3788)
The Ultimate iOS Interview Playbook by Avi Tsadok(3765)
