Alexander Reshytko
Alexander Reshytko

Reputation: 2236

C++ Logical constancy and a returning by value from a const method

There's a class that exposes some of its functionallity and data via another connected class' object returned by value. For example a container type that returns iterators class through begin() and end() methods. It returns iterator by value so

iterator begin();
const iterator begin() const;

won't work and we need two separate classes iterator and const_iterator but then the principle of code reusing is being violated as we need to implement similar functionality in both classes.

Are there any workarounds? How to find a compromise between code reuse and reserving a constancy?

Upvotes: 1

Views: 164

Answers (1)

Tony The Lion
Tony The Lion

Reputation: 63200

No. I have never seen it done any other way than creating const and non-const versions of functions and classes.

Upvotes: 1

Related Questions