/* how to overload the operation [] ,subscipt of 2D-array?
* how can I get element through piece[i][j],rather than piece(i)[j]?
* */
#include"global.h"
using namespace std;
class Piece
{
public:
Piece()
{
for(size_t i = 0;i<2;i++)
for(size_t j = 0;j<3;j++)
{
piece[i][j] = i+ j;
}
for(size_t i=0;i<2;i++)
{
std::copy(piece[i],piece[i]+3,ostream_iterator<int>(cout," "));
cout<<endl;
}
cout<<"----------------------"<<endl;
}
int* operator()(const int& t)//change operator() to operator[] is
wrong ,why?
{
i = t; //
return piece[i];
}
int operator[](const int & t)
{
return this->operator()(i)[t];
}
private:
int i;//witchout varible i,how can I implement overload the () , []
or[],[]
int piece[2][3];
};
//----------------------------------------
int main()
{
Piece p ;
for(int i=0;i<2;i++)
{
for(int j=0;j<3;j++)
{
cout<<p(i)[j]<<" ";//p[i][j],??
}
cout<<endl;
}
}
/*
//output is
0 1 2
1 2 3
----------------------
0 1 2
1 2 3
*/ 5 4612
DaVinci wrote: /* how to overload the operation [] ,subscipt of 2D-array? * how can I get element through piece[i][j],rather than piece(i)[j]? * */
This is a FAQ, http://www.parashift.com/c++-faq-lite/.
Jonathan
DaVinci wrote: /* how to overload the operation [] ,subscipt of 2D-array? * how can I get element through piece[i][j],rather than piece(i)[j]? * */ #include"global.h" using namespace std; class Piece { public: Piece() { for(size_t i = 0;i<2;i++) for(size_t j = 0;j<3;j++) { piece[i][j] = i+ j; } for(size_t i=0;i<2;i++) { std::copy(piece[i],piece[i]+3,ostream_iterator<int>(cout," ")); cout<<endl; } cout<<"----------------------"<<endl; } int* operator()(const int& t)//change operator() to operator[] is wrong ,why? { i = t; // return piece[i]; } int operator[](const int & t) { return this->operator()(i)[t]; } private: int i;//witchout varible i,how can I implement overload the () , [] or[],[] int piece[2][3]; }; //---------------------------------------- int main() { Piece p ; for(int i=0;i<2;i++) { for(int j=0;j<3;j++) { cout<<p(i)[j]<<" ";//p[i][j],?? } cout<<endl; } } /* //output is 0 1 2 1 2 3 ---------------------- 0 1 2 1 2 3 */
I recommend against using the method posted in the C++ FAQ.
It recommends you use a non-standard syntax.
If you want to use standard syntax check out the following link for an
example: http://code.axter.com/dynamic_2d_array.h
However, for most requirements, I recommend using a vector of vector.
Example:
int col = 123;
int row = 456;
vector<vector<int> > My2dArray(col, vector<int>(row));
You can reference both the above vector code and the dynamic_2d_array
class using double index ([][])
My2dArray[0][0] = 99;
Check out the following link for wrapper classes using vector of
vector: http://www.codeguru.com/forum/showthread.php?t=231046 http://www.codeguru.com/forum/showth...hreadid=297838
Axter wrote: DaVinci wrote: /* how to overload the operation [] ,subscipt of 2D-array? * how can I get element through piece[i][j],rather than piece(i)[j]? * */ #include"global.h" using namespace std; class Piece { public: Piece() { for(size_t i = 0;i<2;i++) for(size_t j = 0;j<3;j++) { piece[i][j] = i+ j; } for(size_t i=0;i<2;i++) { std::copy(piece[i],piece[i]+3,ostream_iterator<int>(cout," ")); cout<<endl; } cout<<"----------------------"<<endl; } int* operator()(const int& t)//change operator() to operator[] is wrong ,why? { i = t; // return piece[i]; } int operator[](const int & t) { return this->operator()(i)[t]; } private: int i;//witchout varible i,how can I implement overload the () , [] or[],[] int piece[2][3]; }; //---------------------------------------- int main() { Piece p ; for(int i=0;i<2;i++) { for(int j=0;j<3;j++) { cout<<p(i)[j]<<" ";//p[i][j],?? } cout<<endl; } } /* //output is 0 1 2 1 2 3 ---------------------- 0 1 2 1 2 3 */
I recommend against using the method posted in the C++ FAQ. It recommends you use a non-standard syntax.
If you want to use standard syntax check out the following link for an example: http://code.axter.com/dynamic_2d_array.h
However, for most requirements, I recommend using a vector of vector. Example: int col = 123; int row = 456; vector<vector<int> > My2dArray(col, vector<int>(row));
You can reference both the above vector code and the dynamic_2d_array class using double index ([][]) My2dArray[0][0] = 99;
Check out the following link for wrapper classes using vector of vector: http://www.codeguru.com/forum/showthread.php?t=231046 http://www.codeguru.com/forum/showth...hreadid=297838
yes,I know vector<vector<int> > but here
The class Piece 's purpose 's isnot just for 2D-array,it was desiged to
support some other
operation such as void draw_grahic(),
void move_left() and so on.
But I must first Encapsulate the 2d-array int piece[2][3] as private
data.
and supply operator[][] to get it's data,
is there way to overload the operator[] twice to meet my need?
DaVinci wrote: The class Piece 's purpose 's isnot just for 2D-array,it was desiged to support some other operation such as void draw_grahic(), void move_left() and so on. But I must first Encapsulate the 2d-array int piece[2][3] as private data. and supply operator[][] to get it's data, is there way to overload the operator[] twice to meet my need?
If that's what you really want, just make an operator[] that returns a
proxy. Then, put another operator[] in that proxy that gives a value:
# include <vector>
# include <cstddef>
class test;
class proxy
{
friend class test;
public:
int operator[](std::size_t index)
{
return v_[index];
}
private:
proxy(std::vector<int>& v)
: v_(v)
{
}
std::vector<int>& v_;
};
class test
{
public:
proxy operator[](std::size_t index)
{
return proxy(v_[index]);
}
private:
std::vector< std::vector<int> > v_;
};
int main()
{
test t;
int i = t[1][2];
}
This is completly artificial, since std::vector already has a subscript
operator, but you should get the picture.
Jonathan
DaVinci wrote: Axter wrote:
DaVinci wrote: /* how to overload the operation [] ,subscipt of 2D-array? * how can I get element through piece[i][j],rather than piece(i)[j]? * */ #include"global.h" using namespace std; class Piece { public: Piece() { for(size_t i = 0;i<2;i++) for(size_t j = 0;j<3;j++) { piece[i][j] = i+ j; } for(size_t i=0;i<2;i++) { std::copy(piece[i],piece[i]+3,ostream_iterator<int>(cout," ")); cout<<endl; } cout<<"----------------------"<<endl; } int* operator()(const int& t)//change operator() to operator[] is wrong ,why? { i = t; // return piece[i]; } int operator[](const int & t) { return this->operator()(i)[t]; } private: int i;//witchout varible i,how can I implement overload the () , [] or[],[] int piece[2][3]; }; //---------------------------------------- int main() { Piece p ; for(int i=0;i<2;i++) { for(int j=0;j<3;j++) { cout<<p(i)[j]<<" ";//p[i][j],?? } cout<<endl; } } /* //output is 0 1 2 1 2 3 ---------------------- 0 1 2 1 2 3 */
I recommend against using the method posted in the C++ FAQ. It recommends you use a non-standard syntax.
If you want to use standard syntax check out the following link for an example: http://code.axter.com/dynamic_2d_array.h
However, for most requirements, I recommend using a vector of vector. Example: int col = 123; int row = 456; vector<vector<int> > My2dArray(col, vector<int>(row));
You can reference both the above vector code and the dynamic_2d_array class using double index ([][]) My2dArray[0][0] = 99;
Check out the following link for wrapper classes using vector of vector: http://www.codeguru.com/forum/showthread.php?t=231046 http://www.codeguru.com/forum/showth...hreadid=297838
yes,I know vector<vector<int> > but here
The class Piece 's purpose 's isnot just for 2D-array,it was desiged to support some other operation such as void draw_grahic(), void move_left() and so on. But I must first Encapsulate the 2d-array int piece[2][3] as private data. and supply operator[][] to get it's data, is there way to overload the operator[] twice to meet my need?
Yes.
Did you look at the first link I posted? http://code.axter.com/dynamic_2d_array.h
The above link doesn't use vector vector, and it's able to use
operator[] to return a 2D array without using a proxy class. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Steve |
last post by:
Hi, I hope someone can help.
I have a class called cField, and another class called cFieldList.
cFieldList contains a std::vector of cFields called myvec
I've overloaded the subscript...
|
by: Steve |
last post by:
Hi, I have a class called cList as so:
template<class T> class cList { // base class for Lists
private:
protected:
vector<T> tListOf; // field list container
public:
void Add(const T& t)...
|
by: Jason |
last post by:
Dear All,
I originally have a look up table of size UInt8.
However, there is a restriction that just 1D look up table of size
UInt8 can be use. The no. of UInt8 tables to be used is not...
|
by: olson_ord |
last post by:
Hi,
I am not exactly new to C++, but I have never done operator
overloading before.
I have some old code that tries to implement a Shift Register - but I
cannot seem to get it to work. Here's a...
|
by: Lorn |
last post by:
Hopefully someone can help me out with this problem I'm having with 2d
vectors. My vector initialization looks like this:
struct Object {
double d1;
double d2;
int i1;
};
...
|
by: pasa_1 |
last post by:
The following code results in Segmentation fault
=========================
#include <iostream>
using namespace std;
class abc{
|
by: murali |
last post by:
hello everybody...
how can i overload the subscript operator with more than one
dimension... like ....
if possible please give an example...
thank you...
|
by: Norman Diamond |
last post by:
In an antique obsolete version of MFC, a CString expression could be
subscripted in order to retrieve one element.
Visual Studio 2005 defines CSimpleStringT::operator. At first glance it
looks...
|
by: brandon01 |
last post by:
Keep getting "subscript out of range" any idea why? This function is looped btw.. Thx...
Private Sub getBday()
bDay = List1.List(nextBday)
bDayArr = Split(bDay, " - ")
Text1.Text =...
|
by: C++Liliput |
last post by:
I have a custom String class that contains an embedded char* member.
The copy constructor, assignment operator etc. are all correctly
defined. I need to create a map of my string (say a class...
|
by: ryjfgjl |
last post by:
ExcelToDatabase: batch import excel into database automatically...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM).
In this month's session, we are pleased to welcome back...
|
by: jfyes |
last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
|
by: ArrayDB |
last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
|
by: PapaRatzi |
last post by:
Hello,
I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
|
by: CloudSolutions |
last post by:
Introduction:
For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
|
by: Shællîpôpï 09 |
last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
|
by: Faith0G |
last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome former...
| |