Casting to const.

Discussion in 'C++' started by Jason Heyes, Dec 9, 2003.

  1. Jason Heyes

    Jason Heyes Guest

    I didn't want to repeat the same code so I casted to const. Here is a
    simplified example of when casting to const can help avoid code duplication:

    class MyClass
    {
    int numbers[50];

    public:
    // default constructor not shown

    int &find_largest()
    {
    return ((const MyClass &)*this).find_largest();
    }

    const int &find_largest() const; // implementation not shown
    };

    Should I be doing this? Is there a better way?

    I wanted to do this in another situation but it doesn't look possible. Here
    it is:

    class MyOtherClass
    {
    std::vector<int> numbers;

    public:
    // default constructor not shown

    void find_even(std::vector<int *> &even) {
    for (std::vector<int>::size_type i=0; i < numbers.size(); i++) {
    if (numbers % 2)
    even.push_back(&numbers);
    }
    }

    void find_even(std::vector<const int *> &even) const {
    for (std::vector<int>::size_type i=0; i < numbers.size(); i++) {
    if (numbers % 2)
    even.push_back(&numbers);
    }
    }
    };

    Notice that the code in both methods is exactly the same. How do I avoid
    this kind of duplication? Thanks.
     
    Jason Heyes, Dec 9, 2003
    #1
    1. Advertising

  2. Jason Heyes

    Jason Heyes Guest

    I just realised that I could have written the following.

    class MyClass
    {
    int numbers[50];
    int &find_largest_private() const; // implementation not shown
    public:
    int &find_largest() { return find_largest_private(); }
    const int &find_largest() const { return find_largest_private(); }
    };

    I don't know if I want to do that though. What do you guys think?
     
    Jason Heyes, Dec 9, 2003
    #2
    1. Advertising

  3. Jason Heyes

    Phlip Guest

    Jason Heyes wrote:

    > I just realised that I could have written the following.
    >
    > class MyClass
    > {
    > int numbers[50];
    > int &find_largest_private() const; // implementation not shown
    > public:
    > int &find_largest() { return find_largest_private(); }
    > const int &find_largest() const { return find_largest_private(); }
    > };
    >
    > I don't know if I want to do that though. What do you guys think?


    I thought of this summer (coming back to C++ after refactoring duplication
    out of Python and such mercilessly), and was waiting for a way to illustrate
    it.

    Thanks! it's perfect! It illustrates these design goals:

    - don't return mutable handles to private data
    inside constant objects

    - don't duplicate behavior (your "implementation")

    --
    Phlip
    http://www.c2.com/cgi/wiki?TestFirstUserInterfaces
     
    Phlip, Dec 9, 2003
    #3
  4. Jason Heyes

    Ron Natalie Guest

    "Jason Heyes" <> wrote in message news:3fd56692$0$1019$...
    > I just realised that I could have written the following.
    >
    > class MyClass
    > {
    > int numbers[50];
    > int &find_largest_private() const; // implementation not shown


    It's not clear what find_largest_const is going to return.
    It can't return a reference to something in numbers without some headstanding
    because the type of numbers is effecitvely const int[50] inside a const method.

    How about
    class MyClass {
    int find_largest_private_index() const; // returns the index 0...50 of the larges

    int& find_largest() { return numbers[find_largest_private_index()] };
    const int& find_largest() const { return numbers[find_largest_private_index()] };
     
    Ron Natalie, Dec 9, 2003
    #4
  5. Jason Heyes

    Jason Heyes Guest

    "Ron Natalie" <> wrote in message
    news:3fd59294$0$224$...
    >
    > It's not clear what find_largest_const is going to return.
    > It can't return a reference to something in numbers without some

    headstanding
    > because the type of numbers is effecitvely const int[50] inside a const

    method.
    >
    > How about
    > class MyClass {
    > int find_largest_private_index() const; // returns the index

    0...50 of the larges
    >
    > int& find_largest() { return numbers[find_largest_private_index()] };
    > const int& find_largest() const { return

    numbers[find_largest_private_index()] };

    Yes my mistake. I forgot that numbers was const within
    find_largest_private(). Thanks for that.
     
    Jason Heyes, Dec 9, 2003
    #5
  6. Jason Heyes wrote:

    > I just realised that I could have written the following.
    >
    > class MyClass
    > {
    > int numbers[50];
    > int &find_largest_private() const; // implementation not shown
    > public:
    > int &find_largest() { return find_largest_private(); }
    > const int &find_largest() const { return find_largest_private(); }
    > };
    >
    > I don't know if I want to do that though. What do you guys think?
    > ...


    This is no better than your original approach (which, BTW, is an
    acceptable way to avoid code duplication), since in general case you'll
    eventually discover that you can't return a non-const 'int&' from a
    const method 'find_largest_private' without an explicit 'const_cast'.

    --
    Best regards,
    Andrey Tarasevich
     
    Andrey Tarasevich, Dec 9, 2003
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    11
    Views:
    1,130
  2. Javier
    Replies:
    2
    Views:
    593
    James Kanze
    Sep 4, 2007
  3. Alex Vinokur
    Replies:
    9
    Views:
    817
    James Kanze
    Oct 13, 2008
  4. 0m
    Replies:
    26
    Views:
    1,148
    Tim Rentsch
    Nov 10, 2008
  5. fungus
    Replies:
    13
    Views:
    907
    fungus
    Oct 31, 2008
Loading...

Share This Page