C Board  

Go Back   C Board > General Programming Boards > C++ Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 07-21-2008, 02:40 PM   #1
Registered User
 
Join Date: Jul 2008
Posts: 6
method, once public const and once private non-const

Hi,

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   Reply With Quote
Old 07-21-2008, 02:50 PM   #2
Afraid of widths
 
medievalelks's Avatar
 
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   Reply With Quote
Old 07-21-2008, 04:35 PM   #3
Senior software engineer
 
brewbuck's Avatar
 
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   Reply With Quote
Old 07-22-2008, 07:57 AM   #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   Reply With Quote
Old 07-22-2008, 09:02 AM   #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:
Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
Quoted more than 1000 times (I hope).
anon is offline   Reply With Quote
Old 07-22-2008, 09:52 AM   #6
Afraid of widths
 
medievalelks's Avatar
 
Join Date: Apr 2008
Location: Chicago
Posts: 887
Quote:
Originally Posted by seson View Post
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.
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   Reply With Quote
Old 07-22-2008, 01:16 PM   #7
Cat without Hat
 
CornedBee's Avatar
 
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   Reply With Quote
Reply

Tags
const, iterators, reuse, vector

Thread Tools
Display Modes

Forum Jump


All times are GMT -6. The time now is 10:47 AM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.0 RC2

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22