Public Group

This topic is 4868 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## 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 on other sites
if you properly inline op[] then the compiler should (during release builds) produce the exact same code for both accesses.

##### 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 on other sites
either that or just put it inside the class like
struct foo{  void ImImplicitlyInlined(){/* do stuff */}};

##### Share on other sites
Thanks. Are all class functions implicitly inlined?

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

##### Share on other sites
Quote:
 Original post by mike74Thanks. 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 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 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 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 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/

1. 1
Rutin
36
2. 2
3. 3
4. 4
5. 5

• 11
• 15
• 12
• 14
• 9
• ### Forum Statistics

• Total Topics
633353
• Total Posts
3011486
• ### Who's Online (See full list)

There are no registered users currently online

×

## Important Information

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!