Passing a const object by reference.

Discussion in 'C++' started by Guest, Jan 28, 2006.

  1. Guest

    Guest Guest

    Hey all.

    I just changed from this:

    mat4& operator = (mat4 & mymat4);

    to this:

    mat4& operator = (mat4 const& mymat4);

    all over my different classes, as some compilers compiled without the const, but I learned that it is necessary.

    Now, I get this error:

    binary '[' : no operator found which takes a left-hand operand of type 'const mat4' (or there is no acceptable conversion)

    Here is the function implementation:

    mat4& mat4::eek:perator = (mat4 const& mymat4)
    {
    m[0] = mymat4[0]; m[4] = mymat4[4]; m[8] = mymat4[8]; m[12] = mymat4[12];
    m[1] = mymat4[1]; m[5] = mymat4[5]; m[9] = mymat4[9]; m[13] = mymat4[13];
    m[2] = mymat4[2]; m[6] = mymat4[6]; m[10] = mymat4[10]; m[14] = mymat4[14];
    m[3] = mymat4[3]; m[7] = mymat4[7]; m[11] = mymat4[11]; m[15] = mymat4[15];
    return *this;
    }

    And here is the definition of the [] operator:

    float& mat4::eek:perator [] (int subscript)
    {
    return m[subscript];
    }

    Anyone understand this error?


    Thanks!
     
    Guest, Jan 28, 2006
    #1
    1. Advertising

  2. Guest

    Pete C Guest

    <dontspam@_dylan_.gov> wrote:
    > And here is the definition of the [] operator:
    > float& mat4::eek:perator [] (int subscript)
    > {
    > return m[subscript];
    > }


    You need to define another operator[], but const - like this:

    const float& mat4::eek:perator [] (int subscript) const
    {
    return m[subscript];
    }

    This version of the function will be called by the (const)
    right-hand-side of all your assignments.
    This may or may not compile - I don't know, because you haven't told us
    the type of the 'm' member variable. But give it a try.
     
    Pete C, Jan 28, 2006
    #2
    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. Guest
    Replies:
    1
    Views:
    412
    Bob Hairgrove
    Jan 31, 2006
  2. Replies:
    11
    Views:
    1,124
  3. Javier
    Replies:
    2
    Views:
    585
    James Kanze
    Sep 4, 2007
  4. George2
    Replies:
    10
    Views:
    613
    Pete Becker
    Dec 17, 2007
  5. Arv
    Replies:
    15
    Views:
    940
    James Kanze
    Mar 7, 2008
Loading...

Share This Page