Thread: method, once public const and once private non-const

  1. #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

  2. #2
    Registered User
    Join Date
    Apr 2008
    Posts
    890
    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?

  3. #3
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    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.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  4. #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

  5. #5
    The larch
    Join Date
    May 2006
    Posts
    3,573
    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.

    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).

  6. #6
    Registered User
    Join Date
    Apr 2008
    Posts
    890
    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?

  7. #7
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    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

Popular pages Recent additions subscribe to a feed

Tags for this Thread