![]() |
| | #1 |
| Registered User Join Date: Jul 2008
Posts: 6
| method, once public const and once private non-const Within my class, I have a method that returns a const_interator the the internal vector (a public method marked const). This is ok, because no one using my class can change the internal vector. Now I need that exact same code again, but with a non-const i.e. a normal iterator internally. Sure, I could just duplicate the method, mark it private and remove the const word. How can I reuse one of them in the other one? Code duplication is not so cool. Sebastian |
| seson is offline | |
| | #2 |
| Afraid of widths Join Date: Apr 2008 Location: Chicago
Posts: 887
| I'm guessing you don't need the public one if you refactor your code properly. Why are outside classes peeking at your member data? |
| medievalelks is offline | |
| | #3 |
| Senior software engineer Join Date: Mar 2007 Location: Portland, OR
Posts: 5,381
| An iterator can be converted to a const_iterator, so just have your public method call the private one, and return the iterator the private version returned (as const_iterator of course). It will automatically convert.
__________________ "Congratulations on your purchase. To begin using your quantum computer, set the power switch to both off and on simultaneously." -- raftpeople@slashdot |
| brewbuck is offline | |
| | #4 |
| Registered User Join Date: Jul 2008
Posts: 6
| brewbuck: But my public method is a const one, not so the private method. Const methods can only call other const methods and I want to avoid a const_cast. So this approach fails.. medievalelks: I do need it, my internal data structure is just a set of points, so "everyone" outside can read all points, just not modify them. Sebastian |
| seson is offline | |
| | #5 | |
| The larch Join Date: May 2006
Posts: 3,086
| I assume that there must be some amount of code that doesn't modify the object in common. You might be able to extract that into a constant method and reuse that to implement both the private and public function. E.g (the helper function goes through size_t to avoid problems with constness if an iterator is to be returned directly): Code: class X
{
std::vector<int> v;
public:
typedef std::vector<int>::iterator iterator;
typedef std::vector<int>::const_iterator const_iterator;
X() { std::generate_n(std::back_inserter(v), 10, rand); }
const_iterator special() const { return v.begin() + do_special(); }
private:
iterator special_() { return v.begin() + do_special(); }
std::size_t do_special() const {
return std::max_element(v.begin(), v.end()) - v.begin();
}
};
__________________ I might be wrong. Quote:
| |
| anon is offline | |
| | #6 |
| Afraid of widths Join Date: Apr 2008 Location: Chicago
Posts: 887
| To get an idea of the problem domain, what are some of your class names? e.g., what is the one with the points, and what are some of its clients? |
| medievalelks is offline | |
| | #7 |
| Cat without Hat Join Date: Apr 2003
Posts: 8,439
| There's nothing wrong with a const_cast when used with the "implement const getter in terms of non-const getter" idiom. Scott Meyers uses it in Effective C++.
__________________ All the buzzt! CornedBee"There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code." - Flon's Law |
| CornedBee is offline | |
![]() |
| Tags |
| const, iterators, reuse, vector |
| Thread Tools | |
| Display Modes | |
|