Sign in to follow this  
mike74

operator[] overloading

Recommended Posts

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/

Share this post


Link to post
Share on other sites
Thanks. Let's say I declare it like so:

inline float& operator[](int index)
{
return e[index];
}

Is that considered properly inlined?

Mike C.
http://www.coolgroups.com/

Share this post


Link to post
Share on other sites
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(){}

Share this post


Link to post
Share on other sites
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/

Share this post


Link to post
Share on other sites
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


Share this post


Link to post
Share on other sites
provide a const version of operator [], something like:

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.

Share this post


Link to post
Share on other sites
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/

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this