>I simply have to overload square brackets in order for a program to work.
Why? It's considerably easier to overload the () operator to take two arguments instead of hack your way to a solution with the [] operator. But if you must know, you need to use a helper class:
// Pseudocode
template <typename T>
class helper {
public:
const T operator[](int j) const;
T& operator[](int j);
};
template <typename T>
class matrix {
public:
const helper operator[](int i) const;
helper& operator[](int i);
};
Narue
Bad Cop
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
Dave Sinkula
long time no c
5,058 posts since Apr 2004
Reputation Points: 2,780
Solved Threads: 314
>Well coz I want the convenience
It's not as convenient as you seem to think it is. Why aren't you using vectors anyway? You get the "convenient" syntax out of it and it'll probably be a lot more efficient that anything you can write.
Narue
Bad Cop
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
>Besides it's too late to change my code now.
Why? Has the military started accepting code from newbies and you've passed your deadline? "It's too late to change my code" is a really lame excuse for shoddy workmanship. In fact, if you change your code, you learn more than if you hadn't. Since you're new to C++, that's a very good thing. But no, you're too stubborn to change your code for the better. Congratulations! You're well on your way to mediocrity.
Narue
Bad Cop
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
>I'm not stubborn, just a victim of time constraints!
I have yet to see a class project that doesn't give you more than enough time to finish. If you're approaching the deadline then it's your own fault for either being too lazy to do the work, or too braindead to get a simple solution working before trying to do something fancy. Let that be the lesson learned for this project.
Narue
Bad Cop
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
>@Narue:
I don't know of any good ones. Your best bet is to wander over to a good reference like Dinkumware and use that to figure things out. Or you could get a book on the subject. The C++ Programming Language covers standard containers in detail, and The C++ Standard Library - A Tutorial and Reference is probably the best book that covers the standard library exclusively.
Narue
Bad Cop
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401