Questions tagged [subscript-operator]

53 questions
38
votes
2 answers

Accessing arrays by index[array] in C and C++

There is this little trick question that some interviewers like to ask for whatever reason: int arr[] = {1, 2, 3}; 2[arr] = 5; // does this line compile? assert(arr[2] == 5); // does this assertion fail? From what I can understand, a[b] gets…
NullUserException
  • 77,975
  • 25
  • 199
  • 226
22
votes
6 answers

Swift operator `subscript` []

I am beginner with the Swift having no advance knowledge with operators. I have the following class class Container { var list: [Any] = []; } I want to implement the operator subscript [] in order to access the data from list. I need something…
Colateral
  • 1,517
  • 13
  • 21
10
votes
4 answers

Overloading subscript operator "["

I am trying to overload the subscript operator ("[") for a custom class I've created. I am trying to figure out how to deal with the following issues. How can you figure out if the operator is on lhs or rhs ? i.e. a[x] = foo vs foo = a[x] When…
Pavan Yalamanchili
  • 11,851
  • 2
  • 31
  • 54
9
votes
3 answers

is int[pointer-to-array] in the C++ - standard?

As I have learned, one can write the following code: char *a = new char[50]; for (int i = 0; i < 50; ++i) { i[a] = '5'; } It compiles. It works. It does exactly the same as char *a = new char[50]; for (int i = 0; i < 50; ++i) { a[i] =…
styrofoam fly
  • 498
  • 1
  • 9
  • 22
7
votes
7 answers

Overloading the subscript operator "[ ]" in the l-value and r-value cases

I have overloaded [] operator in my class Interval to return minutes or seconds. But I am not sure how to assign values to minutes or second using [] operator. For example : I can use this statement cout << a[1] << "min and " << a[0] << "sec" <<…
Searock
  • 5,488
  • 8
  • 58
  • 95
6
votes
1 answer

Do pointers support "array style indexing"?

(Self-answered Q&A - this matter keeps popping up) I assume that the reader is aware of how pointer arithmetic works. int arr[3] = {1,2,3}; int* ptr = arr; ... *(ptr + i) = value; Teachers/C books keep telling me I shouldn't use *(ptr + i) like in…
Lundin
  • 155,020
  • 33
  • 213
  • 341
6
votes
2 answers

Do std::strings end in '\0' when initialized with a string literal?

I know string objects aren't null terminated but why should this work? std::string S("Hey"); for(int i = 0; S[i] != '\0'; ++i) std::cout << S[i]; So the constructor copies the null terminator as well, but does not increment the length? Why does…
nek28
  • 229
  • 2
  • 6
6
votes
1 answer

Struct type "does not provide a subscript operator"

I am trying to read values from a file into an array of structs. However, I keep getting compiler errors that tell me that my struct, Books, does not provide a subscript operator and I am lost. The struct is contained in a header file while…
jshapy8
  • 1,733
  • 5
  • 23
  • 51
6
votes
3 answers

Stack overflow when defining subscript on CKRecord in Swift

This question asks whether one can use subscripting with CKRecord in Swift. While I already knew how to do what the questioner wanted, every permutation of it gives me a stack overflow: subscript(key: String) -> CKRecordValue? { get { …
Gregory Higley
  • 14,796
  • 8
  • 59
  • 89
5
votes
1 answer

Why does operator[] only take one argument?

There are plenty of questions related to operator[] only taking one argument, but I can't find one that actually says why. For example, it seems a very natural extension of the language to have matrix[0, 3] call an ElementT& operator[](SizeT x,…
user673679
  • 1,196
  • 1
  • 16
  • 32
4
votes
5 answers

How do I access a C++ subscript operator from within the class in which it resides?

Where, ClassA has an operator as such, that returns ClassB: class ClassA { public: ClassA(); ClassB &operator[](int index); } If I want to access said operator from within ClassA's constructor, as so: ClassA::ClassA() { // How do I…
Nick Bolton
  • 34,516
  • 66
  • 162
  • 230
4
votes
7 answers

Can C++'s operator[] take more than one argument?

Is it possible to define an overloaded operator[] that takes more than one argument? That is, can I define operator[] as follows: //In some class double operator[](const int a, const int b){ return big_array[a+offset*b];} and later use it…
Dan
  • 10,845
  • 11
  • 46
  • 75
4
votes
2 answers

What type does C++ expect for array subscripts?

In C, array subscription: a[b] is merely the syntactic sugar equivalent of dereferencing after pointer arithmetic: *(a+b) (as explained, say, here). How is array subscription interpreted in C++, for base types? (Not for classes, for which we have…
einpoklum
  • 86,754
  • 39
  • 223
  • 453
3
votes
4 answers

Why does std::queue not have operator[]?

The std::queue is implemented with a deque by default. std::deque has the subscript operator, operator[], and is probably implemented with arrays. So why doesn't std::queue have operator[]? I realize you could have a queue with a list as the…
David Winiecki
  • 3,757
  • 2
  • 31
  • 39
3
votes
2 answers

Writing a subscript non-member function

I'm guessing this just isn't legal in C++, but I thought I'd ask, given a struct that I don't own: struct foo { int x; int y; int z; }; I want to write a non-member subscript operator for it: int& operator [](foo& lhs, const std::size_t…
1
2 3 4