How to avoid operator's or method's code duplication for const and non-const objects? [dup

<strong>Possible Duplicate:</strong> How do I remove code duplication between similar const and non-const member functions?

My task is to implement c++ vector analogue. I've coded operator[] for 2 cases.

T myvector::operator[](size_t index) const {//case 1, for indexing const vector return this->a[index]; } T & myvector::operator[](size_t index) {//case 2, for indexing non-const vector and assigning values to its elements return this->a[index]; }

As you can see, the code is completely equal. It's not a problem for this example (only one codeline), but what should I do if I need to implement some operator or method for both const and non-const case and return const or reference value, respectively? Just copy-paste all the code everytime I make changes in it?


One of the few good uses of const_cast here. Write your non const function as normal, then write your const function like so:

const T & myvector::operator[](size_t index) const { myvector<T> * non_const = const_cast<myvector<T> *>(this); return (*non_const)[index]; }


  • C++ Any benifits using pointer to vector of pointers?
  • Passing template typedef as argument to a function template
  • converting a Python list to an R numeric vector
  • H2ORandomForestEstimator with min_samples_split?
  • std::move on a std::future stored in a pair
  • How to chain promises in Clojure
  • How to sanitize input with PHP and the sqlsrv driver?
  • Equivalent of [[NSWorkspace sharedWorkspace] runningApplications] for iphone / iOS?
  • how to insert new rows with values in the same sheet of an excel file in java
  • Index Multiple Columns w/ Ruby on Rails
  • how to sort an arraylist that contains string arrays?
  • a concept similar to pointers in as3?
  • LibGdx GLES2.0 cube texel stretching
  • custom string delimiters stringtemplate-4
  • why my app based on boost::asio didn't accept new connection
  • NSMutableArray instance used in a block
  • Wrap C++ function using Boost Reflect or another C++ reflection library
  • crash in __tcf_0
  • For loop with if condition on multiple R functions
  • RxJava debounce by arbitrary value
  • Unable to decode certificate at client new X509Certificate2()
  • Content-Length header not returned from Pylons response
  • why overloaded new operator is calling constructor even I am using malloc inside overloading functio
  • PHPUnit_Framework_TestCase class is not available. Fix… - Makegood , Eclipse
  • How to model a transition system with SPIN
  • How to make Safari send if-modified-since header?
  • Rearranging Cells in UITableView Bug & Saving Changes
  • How to pass list parameters for each object using Spring MVC?
  • PHP: When would you need the self:: keyword?
  • Python: how to group similar lists together in a list of lists?
  • Proper way to use connect-multiparty with express.js?
  • Setting background image for body element in xhtml (for different monitors and resolutions)
  • LevelDB C iterator
  • JaxB to read class hierarchy
  • Is there any way to bind data to data.frame by some index?
  • Django query for large number of relationships
  • Why is Django giving me: 'first_name' is an invalid keyword argument for this function?
  • How can I use `wmic` in a Windows PE script?
  • Conditional In-Line CSS for IE and Others?
  • How to push additional view controllers onto NavigationController but keep the TabBar?