/* 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_iter ator<int>(cout, " "));
cout<<endl;
}
cout<<"----------------------"<<endl;
}
int* operator()(cons t 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 4643
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_iter ator<int>(cout, " ")); cout<<endl; } cout<<"----------------------"<<endl; } int* operator()(cons t 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<i nt> > My2dArray(col, vector<int>(row ));
You can reference both the above vector code and the dynamic_2d_arra y
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_iter ator<int>(cout, " ")); cout<<endl; } cout<<"----------------------"<<endl; } int* operator()(cons t 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<i nt> > My2dArray(col, vector<int>(row ));
You can reference both the above vector code and the dynamic_2d_arra y 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<i nt> > 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::vect or<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_iter ator<int>(cout, " ")); cout<<endl; } cout<<"----------------------"<<endl; } int* operator()(cons t 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<i nt> > My2dArray(col, vector<int>(row ));
You can reference both the above vector code and the dynamic_2d_arra y 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<i nt> > 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 operator for cFieldList as so:
cField& operator(int pos) { return myvec; }
|
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) {tListOf.push_back(t);} // add new object to list
unsigned int Count() { return tListOf.size(); } // number of list items
|
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
limited.
Are there any simple method for me to constructing many 1D look up
tables of UInt8 so that the same result can be obtained as if the
|
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 simpler version of it.
-------------------- main.cpp---------------------------
# include <iostream>
|
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;
};
std::vector<std::vector <Object> > vMain;
| |
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 like it might have been intended to provide backwards compatibility
for antique programs. But there seems to be no way to use it.
CString s = _T("ab");
short i = 1;
_TCHAR c = s;
|
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 = bDayArr(0) ' subscript out of range
Text2.Text = bDayArr(2) ' subscript out of range
nextBday = nextBday + 1
End Sub
|
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 called
MyString) and an integer i.e. std::map<MyString, int>. Whenever I
insert the elements in the map using the subscript operator, I
noticed that the copy constructor for MyString is invoked more number
of times than if I do it using the insert()...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
| |
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
|
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |