473,404 Members | 2,178 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,404 software developers and data experts.

overloading subscript

hello everybody...

how can i overload the subscript operator with more than one
dimension... like [][][]....

if possible please give an example...

thank you...

Sep 10 '06 #1
3 3248
a
Replacing the operators with the explicit function call will make this
clearer:

a[ x][ y ][ z ]
is the same thing as
a.operator[]( x ).operator[]( y ).operator[]( z ).

So the return types of the first 2 subscript operators must be types that
support a subscript operator.

A

Sep 10 '06 #2
In article <11**********************@b28g2000cwb.googlegroups .com>,
mu**********@gmail.com says...
hello everybody...

how can i overload the subscript operator with more than one
dimension... like [][][]....
You can't do it directly -- you can either 1) overload some other
operator (most often ()), or 2) you can overload operator[] to return
some type that itself overloads operator[], and so on until you get to
the point of returning the individual item requested.

In the latter category, you have two general possibilities: use
something like std::vector<std::vector<std::vector<double >, or
create multi-dimension matrix class that overloads operator[] to return
a proxy, which overloads operator[] to return a proxy, [to a depth of
dimensions-1], with the final one actually returning the requested item.

// warnings:
// only minimally tested
// no attempt at const correctness
// no attempt at bounds checking
// only supports cube-shaped 3D matrices
//
template<class T, unsigned size>
class matrix3 {

T data[size][size][size];

friend class proxy;
friend class proxy2;

class proxy {
matrix3 &m_;
unsigned index1_, index2_;
public:
proxy(matrix3 &m, unsigned i1, unsigned i2)
: m_(m), index1_(i1), index2_(i2)
{}

T &operator[](unsigned index3) {
return m_.data[index1_][index2_][index3];
}
};

class proxy2 {
matrix3 &m_;
int index_;
public:
proxy2(matrix3 &m, unsigned d) : m_(m), index_(d) { }

proxy operator[](unsigned index2) {
return proxy(m_, index_, index2);
}
};
public:
proxy2 operator[](unsigned index) {
return proxy2(*this, index);
}
};

Used something like:

This code is intended only to demonstrate the basic concept involved --
as theh warnings hint, it's not suitable for serious use as it stands.

#include <iostream>
#include <iomanip>

int main() {

const int size = 4;

matrix3<double, sizem;

for (int x=0; x<size; x++) // initialize the contents
for (int y = 0; y<size; y++)
for (int z = 0; z<size; z++)
m[x][y][z] = x*100 + y * 10 + z;

for (int a=0; a<size; a++) // print out a diagonal
std::cout << std::setw(3)
<< std::setprecision(3)
<< std::setfill('0')
<< m[a][a][a] << "\n";
return 0;
}

--
Later,
Jerry.

The universe is a figment of its own imagination.
Sep 10 '06 #3
murali <mu**********@gmail.comwrote:
how can i overload the subscript operator with more than one
dimension... like [][][]....

if possible please give an example...
See
http://www.parashift.com/c++-faq-lit...html#faq-13.10

plus the two FAQs following it.

--
Marcus Kwok
Replace 'invalid' with 'net' to reply
Sep 11 '06 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

17
by: Terje Slettebų | last post by:
To round off my trilogy of "why"'s about PHP... :) If this subject have been discussed before, I'd appreciate a pointer to it. I again haven't found it in a search of the PHP groups. The PHP...
15
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...
10
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...
51
by: Pedro Graca | last post by:
I run into a strange warning (for me) today (I was trying to improve the score of the UVA #10018 Programming Challenge). $ gcc -W -Wall -std=c89 -pedantic -O2 10018-clc.c -o 10018-clc...
3
by: md | last post by:
Hi, the following code is working for static objects. ie the statement IntArray x(20); my problem is i want to use this overloading operator for dynamically created objects...
8
by: mural | last post by:
hi everyone.... im new to this group... cheers to all. k then can u suggest me how to overload the 2D subscript operator. i get difficulty on passing the second arguement....
3
by: mural | last post by:
hai all how can i overload the subscript with more than one dimension like .. if possible please give an example.. thank you
6
by: josh | last post by:
Hi I've a dubt! when we have overloaded functions the compiler chooses the right being based on the argument lists...but when we have two subscript overloaded functions it resolves them being...
4
by: gvr123 | last post by:
Hi all This seems to me a peculiar problem, but confounding nonetheless... The problem seems to be that an overloaded subscript operator isn't being called unless it is called explicitly ...
5
by: ashu | last post by:
i have studied this example of overloading operator, but i don`t know exactly what happened in code .kindly explain me: #include <iostream> using namespace std; const int SIZE = 3; class...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
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,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
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...
0
tracyyun
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...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.