Solved

operator[][]

Posted on 2002-03-25
16
301 Views
Last Modified: 2010-04-02
Say u have a class A and a class B and class C;
class A{
public:
   int i;
   .......
   class B{
   int j;  
   class C{
   int  array [200];

  }cObj;
   }bObj[100];

operator[][](int one,int two)
{
   return bObj[one].cObj.array[two];
}
}aObj[50];
main()
{
///This is the code that i wants to write...
aObj[7][8] = 5;
}

How can i define the operator[][]??
0
Comment
Question by:GEliyahu
  • 5
  • 3
  • 3
  • +4
16 Comments
 
LVL 1

Expert Comment

by:hustch
ID: 6893780
You can't define operator[][], because there is no such operator in C++.

But you can get the effect you want this way.

1) Define A::operator[] to return a reference to a B object
2) Define B::operator[] to return a reference to an item in C::array.

I don't know if it will work with nested classes.
0
 

Author Comment

by:GEliyahu
ID: 6893811
Not such operator in C++??
What about the follow code:

int a[6][9];

a[5][3] = 3;

if this legal why cant i do a thing like that?

GEliyahu.
0
 
LVL 4

Expert Comment

by:jos010697
ID: 6893817
'[][]' are _two_ concatenated operators. Given your example:

   int a[6][9]; // array of arrays of ints.

   a[5]         // 6th element of a == array of 9 ints

       [3]      // 4th element a[5] == int

kind regards,

Jos
0
 

Author Comment

by:GEliyahu
ID: 6893819
Not such operator in C++??
What about the follow code:

int a[6][9];

a[5][3] = 3;

if this legal why cant i do a thing like that?

GEliyahu.
0
 
LVL 1

Expert Comment

by:hustch
ID: 6893837
As Jos says, it is legal, but it isn't operator[][], but two times operator[].

The first one returns an array of ints (because a is an array of arrays of int) and the second one returns an int.

What you should do is just the same.
What you got is (in principle) an array of arrays of ...

Each time you define a operator[] you remove one of the "array of", and you just repeat that until there are no more "array of"'s left.
0
 

Author Comment

by:GEliyahu
ID: 6893866
Not clear to me how i am gonna write it.
can you please use my example with classes A,B,C to demonstrate me please?

GEliyahu.
0
 
LVL 1

Expert Comment

by:hustch
ID: 6894000
Here it is:
There is only a few changes,
1) added public: to B and C
2) changed operator[][] to two oprerator[]
3) changed aObj[7][8] to aObj[6][7][8] because there are three arrays.

The first index ([6]) selects your aObj. This uses built-in array handling
The second index ([7]) selects your bObj. This uses B& operator[](int ix)
The third index ([8]) selects your array element this uses int& operator[](int ix)

You can see this if you step into the functions with a debugger.

class A{
public:
  int i;
 
  class B{
  public:
    int j;  
    class C{
    public:
      int  array [200];
    }cObj;
   
    int& operator[](int ix) {
      return cObj.array[ix];
    };
   
  }bObj[100];
 
  B& operator[](int ix)
  {
    return bObj[ix];
  }
 
}aObj[50];



void Test() {
  aObj[6][7][8] = 5;

}
0
 
LVL 1

Expert Comment

by:hustch
ID: 6894017
the public: in B can be moved down before int& operator[](int ix)
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 6

Expert Comment

by:thienpnguyen
ID: 6894560
0
 
LVL 1

Expert Comment

by:Pavlik
ID: 6895268
a[i][j][k]   =>   a.operator[](i).operator[](j).operator[k]

and it is legal for int a[5][6][7] because the type of a[3] is (int)[6][7]

One of the possible solutions can be:

===================================================
class A_ref
{
public:
  int& operator[](int i) {return *(rowStart + i};
  int* rowStart;
};

class A
{
public:
  A_ref operator[](int i);
  int arr[ARR_SIZE];
  int rowSize;
};

A_ref A::operator[](int i)
{
A_ref ar;
ar.rowStart = (arr + i*rowSize);
return ar;
}
========================================
With such implementation you don't have arrays of nested classes.
0
 
LVL 1

Expert Comment

by:Pavlik
ID: 6895270
a[i][j][k]   =>   a.operator[](i).operator[](j).operator[k]

and it is legal for int a[5][6][7] because the type of a[3] is (int)[6][7]

One of the possible solutions can be:

===================================================
class A_ref
{
public:
  int& operator[](int i) {return *(rowStart + i};
  int* rowStart;
};

class A
{
public:
  A_ref operator[](int i);
  int arr[ARR_SIZE];
  int rowSize;
};

A_ref A::operator[](int i)
{
A_ref ar;
ar.rowStart = (arr + i*rowSize);
return ar;
}
========================================
With such implementation you don't have arrays of nested classes.
0
 
LVL 4

Expert Comment

by:IainHere
ID: 6896123
Also, you might like to think about using a(1,2)instead of a[1][2].

0
 
LVL 2

Expert Comment

by:Lockias
ID: 6897526
The best way to do this is with the operator that is meant for exactly this sitiuation: operator().  You can put any number of arguments in there.  Yes, you can piggy-back operator[] and in some cases that is fine, but in many cases the result of the first operator[] is not something you want to return, though you are forced to in order to support the operator[] applied to that returned value.

So, if you decide that you like the idea of using operator(), which would look like:

  value = object(1, 2);

instead of:

  value = object[1,2];

then explore the documentation on this operator and ask for further guidance as needed.

~Lockias
0
 
LVL 1

Expert Comment

by:Pavlik
ID: 6897646
Hi Lockias.

You are right, in most cases operator() is prefferable. But there are some situations where you need multiple operator[]. For example when you don't know number of dimensions at the moment you write your class or when you need access to one element as well as to whole subdimension(Implementation of template for n-dimensional matrix).

0
 
LVL 1

Expert Comment

by:hustch
ID: 6898503
Also, in the given example you would end up with code like
a[2](5,3) = 3;
Which is why i didn't suggest it in the first place.
Otherwise I agree, that you should at least consider operator() in your design.
0
 
LVL 4

Accepted Solution

by:
IainHere earned 50 total points
ID: 6898802
If you want an n-dimensional _resizable_ templated array, then look no further:

http://www.cuj.com/articles/2000/0012/0012c/0012c.htm

A _very_ good article, with the slight drawback that it is much more complicated than any of the suggestions so far proposed.  Have fun.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
std::atomic with custom class 6 187
What does std::atomic give me? 7 113
Would like to move button in a function 3 71
How to split this in C++ 4 93
When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

919 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now