| | |
overloading [] with 2 dimensional arrays
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Dec 2004
Posts: 7
Reputation:
Solved Threads: 0
I simply have to overload square brackets in order for a program to work. I know the syntax for 1-dimensional array. It goes something like this:
class MyArray
{
private:
int *arr;
int size;
public:
MyArray(int s)
{
size = s;
arr = new int[size];
}
int & MyArray operator[](int); // declaring
};
int & MyArray :: operator[](int subscript)
{
assert(0 <= subscript && subscript <= size)
return arr[subscript];
}
void main()
{
MyArray a1(5);
a1[2] = 3;
}
I've included just the basic functions in this. This works fine. I just can't think of how to apply this to a double dimension array. I desperately need to overload [] for a 2 dimensional array. Can somebody please help?
class MyArray
{
private:
int *arr;
int size;
public:
MyArray(int s)
{
size = s;
arr = new int[size];
}
int & MyArray operator[](int); // declaring
};
int & MyArray :: operator[](int subscript)
{
assert(0 <= subscript && subscript <= size)
return arr[subscript];
}
void main()
{
MyArray a1(5);
a1[2] = 3;
}
I've included just the basic functions in this. This works fine. I just can't think of how to apply this to a double dimension array. I desperately need to overload [] for a 2 dimensional array. Can somebody please help?
>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:
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:
C++ Syntax (Toggle Plain Text)
// 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); };
New members chased away this month: 4
C++ Syntax (Toggle Plain Text)
assert(0 <= subscript && subscript <= size)
assert(0 <= subscript && subscript < size);
http://www.parashift.com/c++-faq-lit....html#faq-13.8
Last edited by Dave Sinkula; Dec 29th, 2004 at 4:48 pm. Reason: Added link.
•
•
Join Date: Dec 2004
Posts: 7
Reputation:
Solved Threads: 0
•
•
•
•
Originally Posted by Narue
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.
Vectors - yes. It didn't occur to me to use them as I'm new to C++. Besides it's too late to change my code now.
>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.
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.
New members chased away this month: 4
•
•
Join Date: Dec 2004
Posts: 7
Reputation:
Solved Threads: 0
•
•
•
•
Originally Posted by Narue
"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.
>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.
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.
New members chased away this month: 4
![]() |
Similar Threads
- Addition with Two Dimensional Arrays (C++)
- pointers and multi-dimensional arrays (C++)
- C++ Multi dimensional arrays (C++)
- Multi-dimensional Arrays: (Python)
- three dimensional arrays (PHP)
- How to implement multi-dimensional arrays in Python?? (Python)
Other Threads in the C++ Forum
- Previous Thread: adding data into an char array
- Next Thread: memory
| Thread Tools | Search this Thread |
Tag cloud for C++
api application array arrays assignment beginner binary bitmap c++ c/c++ calculator char char* class classes code coding compile compiler console conversion convert count data database delete developer display dll email encryption error file forms fstream function functions game generator getline givemetehcodez graph homeworkhelper iamthwee ifstream image input int java lazy lib loop looping loops map math matrix memory multidimensional multiple newbie news node number numbertoword output parameter pointer problem program programming project proxy python random read recursion recursive reference return sorting string strings struct template templates text tree url variable vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets






