2

Possible Duplicate:
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?

Community
  • 1
  • 1
karlicoss
  • 2,361
  • 3
  • 22
  • 26

1 Answers1

0

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];
} 
Benjamin Lindley
  • 95,516
  • 8
  • 172
  • 256
  • 1
    This effectively defeats the `const`. This is as bad a use as any. – Potatoswatter Apr 20 '11 at 19:08
  • @Potatoswatter: Of course it defeats the const. That's it's purpose. – Benjamin Lindley Apr 20 '11 at 19:10
  • Post edit: In order to copy the answer to the question this is getting duped to, you need to cast *to* `const` in the non-const method. This way is illegal. – Potatoswatter Apr 20 '11 at 19:12
  • 1
    @Potatoswatter: No, it's not illegal. – Benjamin Lindley Apr 20 '11 at 19:17
  • @Ben: You are creating and potentially using a non-`const` path to a `const` object. That's illegal even if it compiles. – Potatoswatter Apr 20 '11 at 20:16
  • 1
    @Potatoswatter - no, it's not illegal. It's undefined behavior to modify a const object, it's fine to use a non-const pointer or reference to read it. If the non-const `operator[]` modifies the object, then you're in trouble, but here it doesn't. – Steve Jessop Apr 20 '11 at 21:52
  • @Steve: ah, I was thinking there was an `operator*` call in there too. Either way, it's cleaner to implement a non-const function which adds const than to rely on the non-const being a valid const implementation. – Potatoswatter Apr 20 '11 at 22:24
  • @Potatoswatter: true. There's a risk of doing something unsafe whichever you do, though - if const calls non-const then you have to manually ensure the non-const version doesn't modify. If non-const calls const you have to verify that the thing it returns can validly be modified if the object is non-const (for example if T is char, the compiler would let the const version return part of a string literal). Contradicting the answer I gave on the dupe question in 2008, nowadays I'd prefer to write a shared template function, so that the compiler can const-check everything for me. – Steve Jessop Apr 21 '11 at 13:49