discards qualifiers

Discussion in 'C++' started by Chameleon, Nov 17, 2010.

  1. Chameleon

    Chameleon Guest

    #include <vector>

    using namespace std;

    struct Vector
    {
    double x,y,z;
    double &operator[](int i) { return (&x); }
    };


    void here_it_comes(const vector<Vector> &vertex)
    {
    double a = vertex[0][0]; // compile error
    double b = const_cast<Vector&>(vertex[0])[0]; // no problem, but why
    the need not to be const? I change it nowhere.
    }


    int main() { return 0; };
    /*
    1.cpp: In function 'void here_it_comes(const std::vector<Vector,
    std::allocator<Vector> >&)':
    1.cpp:14: error: passing 'const Vector' as 'this' argument of 'double&
    Vector::eek:perator[](int)' discards qualifiers
    */
    Chameleon, Nov 17, 2010
    #1
    1. Advertising

  2. On 11/17/2010 2:22 PM, Chameleon wrote:
    > #include <vector>
    >
    > using namespace std;
    >
    > struct Vector
    > {
    > double x,y,z;
    > double &operator[](int i) { return (&x); }
    > };
    >
    >
    > void here_it_comes(const vector<Vector> &vertex)


    'vertex' refers to a constant vector. That means that the operator[] of
    it returns a reference to a *constant* Vector. Your Vector::eek:perator[]
    is non-const (since it is apparently designed to allow putting it on the
    left side of the assignment operator). Declare another operator[] in
    Vector, like so:

    struct Vector
    {
    ...
    double operator[](int i) const { return (&x); }

    and it's going to be OK.

    > {
    > double a = vertex[0][0]; // compile error
    > double b = const_cast<Vector&>(vertex[0])[0]; // no problem, but why the
    > need not to be const? I change it nowhere.
    > }
    >
    >
    > int main() { return 0; };
    > /*
    > 1.cpp: In function 'void here_it_comes(const std::vector<Vector,
    > std::allocator<Vector> >&)':
    > 1.cpp:14: error: passing 'const Vector' as 'this' argument of 'double&
    > Vector::eek:perator[](int)' discards qualifiers
    > */


    V
    --
    I do not respond to top-posted replies, please don't ask
    Victor Bazarov, Nov 17, 2010
    #2
    1. Advertising

  3. Chameleon

    James Kanze Guest

    On Nov 17, 7:47 pm, Victor Bazarov <> wrote:
    > On 11/17/2010 2:22 PM, Chameleon wrote:


    > > #include <vector>


    > > using namespace std;


    > > struct Vector
    > > {
    > > double x,y,z;
    > > double &operator[](int i) { return (&x); }
    > > };


    > > void here_it_comes(const vector<Vector> &vertex)


    [...]
    > Declare another operator[] in Vector,
    > like so:


    > struct Vector
    > {
    > ...
    > double operator[](int i) const { return (&x); }


    > and it's going to be OK.


    But only if i is 0 (as it is in his test program). Otherwise,
    he has undefined behavior.

    --
    James Kanze
    James Kanze, Nov 18, 2010
    #3
    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. danny van elsen
    Replies:
    6
    Views:
    41,212
    Pete Becker
    Apr 24, 2005
  2. Jason
    Replies:
    6
    Views:
    8,951
    Mike Wahler
    Oct 9, 2003
  3. Charlie Zender
    Replies:
    12
    Views:
    1,068
    Peter Pichler
    Jan 3, 2004
  4. Neo
    Replies:
    1
    Views:
    542
    Victor Bazarov
    Mar 28, 2006
  5. Ian Collins

    discards qualifiers

    Ian Collins, May 7, 2006, in forum: C++
    Replies:
    2
    Views:
    746
    John Carson
    May 7, 2006
Loading...

Share This Page