🔒 Locked
operator[] overloading
Advertisement
Original Post
I'm working on my free matrix library at http://www.coolgroups.com/matrixlib/, and I'm going to add this function to my matrix class: float& operator[](int index) { return e[index]; } The class currently looks like this: struct Matrix { float e[16]; }; Now, my question is... let's say you have a Matrix named m. Is it faster to access, say the 10th element, doing m.e[9] instead of doing m[9]? I would like to do m[9] for cleanliness, but if it's a lot slower, I might as well do m.e[9]. Mike C. http://www.coolgroups.com/
if you properly inline op[] then the compiler should (during release builds) produce the exact same code for both accesses.
either that or just put it inside the class like
struct foo{ void ImImplicitlyInlined(){/* do stuff */}};Quote:
Original post by mike74
Thanks. Are all class functions implicitly inlined?
Mike C.
http://www.coolgroups.com/
If written inline as shown in my previous reply yes.
that is
struct foo{ void ThisWillBeInlined(){/*do stuff*/} void AndThisWillNot(); void ImExplicitilyInlined();};/* later somewhere not to far away */void foo::AndThisWillNot(){}inline void foo:ImExplicitlyInlined(){}
Ahh, nice. Thanks.
One last thing. I have this output function:
ostream& operator<<(ostream& out, const Matrix &m)
{
out << m[0] << " " << m[4] << " " << m[8] << " " << m[12] << endl;
out << m[1] << " " << m[5] << " " << m[9] << " " << m[13] << endl;
out << m[2] << " " << m[6] << " " << m[10] << " " << m[14] << endl;
out << m[3] << " " << m[7] << " " << m[11] << " " << m[15];
return out;
}
However, I get this error:
c:\pointninja\matrixlib.h(45) : error C2678: binary '[' : no operator found whic
h takes a left-hand operand of type 'const Matrix' (or there is no acceptable co
nversion)
If I change the
const Matrix &m
to just
Matrix &m
it works. Is there any way to make it work while retaining the const?
Mike C.
http://www.coolgroups.com/
One last thing. I have this output function:
ostream& operator<<(ostream& out, const Matrix &m)
{
out << m[0] << " " << m[4] << " " << m[8] << " " << m[12] << endl;
out << m[1] << " " << m[5] << " " << m[9] << " " << m[13] << endl;
out << m[2] << " " << m[6] << " " << m[10] << " " << m[14] << endl;
out << m[3] << " " << m[7] << " " << m[11] << " " << m[15];
return out;
}
However, I get this error:
c:\pointninja\matrixlib.h(45) : error C2678: binary '[' : no operator found whic
h takes a left-hand operand of type 'const Matrix' (or there is no acceptable co
nversion)
If I change the
const Matrix &m
to just
Matrix &m
it works. Is there any way to make it work while retaining the const?
Mike C.
http://www.coolgroups.com/
the compiler tells you that it can't call a non-const method
on a const variable
The solution is to declare 2 method
const float & operator [] (int i) const;
and
float & operator [] (int i);
The first method let you call x=m[0]; on const matrix instance
The second let you call m[0]=3.0; but only on non-const matrix instance
on a const variable
The solution is to declare 2 method
const float & operator [] (int i) const;
and
float & operator [] (int i);
The first method let you call x=m[0]; on const matrix instance
The second let you call m[0]=3.0; but only on non-const matrix instance
provide a const version of operator [], something like:
that's a good rule of thumb anyhow if you provide op[] provide both const and non const versions of it.
struct Matrix{float e[16]; float& operator[](size_t n){ return e[n];}//returning by value instead of const& since float is a built in type //feel free to call it a pointless micro-optimization ;)float operator[](size_t n) const { return e[n];}};that's a good rule of thumb anyhow if you provide op[] provide both const and non const versions of it.
Okay. It looks like it worked. On a related note, I've never seen that notation before where a keyword like const appears after the parameters of the function like this:
const float & operator [] (int i) CONST;
What exactly is that telling the compiler? And, are there other keywords that can appear in that position?
Thanks.
Mike C.
http://www.coolgroups.com/
const float & operator [] (int i) CONST;
What exactly is that telling the compiler? And, are there other keywords that can appear in that position?
Thanks.
Mike C.
http://www.coolgroups.com/
it tells the compiler that the function won't alter the class instance for wich the function is called.
Only functions declared like that can be called for const objects.
Only functions declared like that can be called for const objects.
Topic Locked
This topic has been locked by a moderator. New replies are not allowed.
Sign in to reply to this topic.
Advertisement