-1

I have a class A and a member function f, that returns a pointer to some contents of the instance

int* A::f() {
  // common code ...
  return &(this->some_container[some_index])
}

Now, I need an analogue function, which only returns a const pointer, i.e.

const int* A::f_const() const {
  // exactly the same lengthy code ...
  return &(this->some_container[some_index]);
}

Note that, as the "lengthy code" does not change the instance of A, this also makes the function const w.r.t. the instance (the second const statement).

My question: Now I wonder if there is some solution to avoid the code doubling, without using a third function.

On one hand, calling f inside f_const would be illegal, as f is not const w.r.t. the owning instance. On the other hand, calling f_const inside f would require to convert a const pointer to a non-const pointer, which is also impossible.

Is there any elegant declaration method, to avoid code doubling?

As said, using a third function with the common functionality of f and f_const is not an option here, as when applied for a large number of functions, it would make the code less readable.

flonk
  • 43
  • 1
  • 4
  • 1
    Possible duplicate of [How do I remove code duplication between similar const and non-const member functions?](http://stackoverflow.com/questions/123758/how-do-i-remove-code-duplication-between-similar-const-and-non-const-member-func) – Rakete1111 Aug 16 '16 at 14:34
  • Why are you doing this? One never ceases to wonder at the boundlessly inane C++ problems one encounters. Dr. Stroustrup never intended for his "practical joke" of a "language" (just ask him) to serve as a basis for piddling philosophical arguments about what side effects of what mechanisms can result in such-and-such unexpected or unhoped-for semantics. Of course, you could just copy the datum into a static scalar and return its address, which should remain rather constant :-) – Bruce David Wilner Aug 16 '16 at 14:36

1 Answers1

0

I want to premise the answer it could depend on the particular context (for example the length-code of the functions, returned type, etc...).

In general, for my experiences, the most practical, even if maybe a little bit ugly, solution is the const cast operator.

Just to implement the const method and get it with a conversion in the non-const other method. Something like:

const int* A::f_const() const {
  return &(this->some_container[some_index]);
}

int* A::f() {
  return const_cast<int*>(f_const());
}

This is usually safe because you're the designer of the class itself.

BiagioF
  • 9,368
  • 2
  • 26
  • 50
  • Thanks for your constructive answer. It seems the implementation of `f_const` misses a member. Should it simply be `this->f_const()` or does `this` also need to be casted somehow? – flonk Aug 16 '16 at 16:34
  • @flonk Sorry, I cannot understand what do you mean. May you try to explain it again and more? – BiagioF Aug 16 '16 at 16:53
  • Sorry, I was unable to express something which made no sense anyhow, your answer is perfectly fine :) – flonk Aug 16 '16 at 17:02