Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Passing a const object by reference.

Thread Tools

Passing a const object by reference.

Posts: n/a
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: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:perator [] (int subscript)
return m[subscript];

Anyone understand this error?


Reply With Quote
Pete C
Posts: n/a
<> wrote:
> And here is the definition of the [] operator:
> float& mat4:perator [] (int subscript)
> {
> return m[subscript];
> }

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

const float& mat4: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.

Reply With Quote

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
const vector<const MyType> Vs const vector<MyType> C++ 2 02-09-2009 10:45 PM
is const necessary in eg int compar(const void *, const void *) lovecreatesbeauty@gmail.c0m C Programming 26 11-10-2008 09:47 PM
const correctness - should C++ prefer const member over non-const? fungus C++ 13 10-31-2008 05:33 AM
const vector<A> vs vector<const A> vs const vector<const A> Javier C++ 2 09-04-2007 08:46 PM
Casting int'** to 'const int * const * const' dosn't work, why? C Programming 11 07-01-2007 06:16 PM