Click here to Skip to main content
15,891,567 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
In C#, you can do something (i don't know exactly how) like this:
C#
T this[int index]
{
  return array[index];
}

The array is an array of Ts ofcourse (T[])

I tried this in C++, but it did not work:
C++
template <class T> T this[int index] {
  return array[index];
}

Also note that the C++ class it is in is built like this:
C++
template <class T> class AdvArray {
  ...
}

Can somebody please help. This is what i want to do:
C++
AdvArray<int> arr = AdvArray<int>();
arr.Add(17);
arr.Add(38);
arr.Add(45);
int _ar = arr[1]; //To make _ar = 38


What I have tried:

I have tried the above, and i could not think of any other way to make an "indexable" class in this way that you could in C#.
Posted
Updated 22-Nov-21 3:50am

You need to overload the array operator: operator overloading - cppreference.com[^]
 
Share this answer
 
Comments
0x01AA 21-Nov-21 12:17pm    
For c++ templates it is more it has to be implemented by the template class.This is very different from overloading ;)
[Edit]
To make it clear the +5 vote was not from here. I do not vote here and especally I do not vote +5 for a missleading answer.
Richard MacCutchan 21-Nov-21 13:19pm    
Then maybe you could post a solution.
0x01AA 21-Nov-21 13:30pm    
Sorry, nope. I do not know/are familar with the c# part enough :(
Richard MacCutchan 21-Nov-21 14:52pm    
So why did you say my answer was misleading?
0x01AA 21-Nov-21 14:55pm    
Because 'overload operator' has nothing to do with c++ templates. Sorry, I'm doing c++ for over 30 years ;)
To both get and set container elements using an index you need two index operators, like this:
C++
template <class T> AdvArray {
    std::vector<T> array;
public:
    void Add(const T& value) { array.emplace_back(value); }
    T& operator [] (size_t i) { return array[i]; }
    T operator [] (size_t i) const { return array[i]; }
};

The second operator is declared const, and therefore can only be used to read values. The first operator returns a refernce to the stored value and therefore can be used to change values:
C++
AdvArray<int> myarray;
myArray.Add(5); // increase array size and set array[0] to 5
myArray[0] = 6; // change array[0] to 6

If you don't need a setter, you can omit the first operator.
 
Share this answer
 
I fixed it, i saw somebody (username unknown) somewhere which made it like this (and it works with the template):
C++
#include <vector>

template <class T> class AdvArray {
private:
  std::vector<T> array;
  T get(int i) {
    return array[i];
  }
public:
  struct proxy {
    proxy(AdvArray *advArray, int i) : advArray(advArray), i(i) {}
    AdvArray *advArray;
    int i;
    operator T() { return advArray->get(i); }
  };
  proxy operator[](int i) {
    return proxy(this, i);
  }

And it works, flawlessly!
 
Share this answer
 
Comments
Stefan_Lang 22-Nov-21 9:37am    
I neither fully understand the question, nor this solution. Richards answer seemed to be entirely correct. That it neglects to mention you have a templated class doesn't matter in this case.

Your solution does a bad job in multiple aspects: it introduces unclear ownership of heap objects using unmanaged pointers, creates multiple redundant copies of user data, and creates proxy objects that may access invalid or outdated memory.

I'm not sure what purpose your proxy is supposed to fulfill, but as far as I can tell you're better off without it. Just implement the index operator for AdvArray.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900