subscript-operator

Bug in Swift array subscript indexing?

喜欢而已 提交于 2020-01-25 04:28:05
问题 I've isolated some Swift code from my project that can be pasted into a Playground. It produces an error "Could not find an overload for '+' that accepts the supplied arguments" both in normal Xcode editing and the Playground. The error refers to the last (non-trivial) line. import UIKit let points = 40 let max = points-1 let L = 10.0 let Deltat = 0.01 let Deltax = L/Double(points) var a = [Double](count: points, repeatedValue: 0.0) var b = [Double](count: points, repeatedValue: 0.0) var c =

Array Subscription: returning Reference vs proxy class method

北城余情 提交于 2020-01-02 19:28:09
问题 While searching for methods for overloading Subscript('[]') operator for template class, I came across two different techniques. First Technique: Overloading the operator [] returning pointer to the container directly, which will allow both reading value and assigning value. A sample implementation of this technique: template <class T> class X { int _size; T *container; public: X(int sz) { _size=sz; container=new T[sz](); } ~X() { } T& operator [](int indx) { return container[indx]; } }; With

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

℡╲_俬逩灬. 提交于 2019-12-09 17:39:20
问题 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" << endl; but I want to overload [] operator, so that I can even assign values to minutes or seconds using a[1] = 5; a[0] = 10; My code : #include <iostream> using namespace std; class Interval { public: long minutes; long seconds; Interval(long m, long

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

蹲街弑〆低调 提交于 2019-12-04 06:10:00
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" << endl; but I want to overload [] operator, so that I can even assign values to minutes or seconds using a[1] = 5; a[0] = 10; My code : #include <iostream> using namespace std; class Interval { public: long minutes; long seconds; Interval(long m, long s) { minutes = m + s / 60; seconds = s % 60; } void Print() const { cout << minutes << ':' << seconds <

Stack overflow when defining subscript on CKRecord in Swift

☆樱花仙子☆ 提交于 2019-12-04 03:17:47
问题 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 { return objectForKey(key) as CKRecordValue? } set { setObject(newValue, forKey: key) } } The stack overflow occurs in the getter. (I've never tried the setter, so it may occur there, too.) I've tried implementing with objectForKey: , objectForKeyedSubscript: ,

Swift operator `subscript` []

爱⌒轻易说出口 提交于 2019-12-03 15:09:46
问题 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 like this: var data: Container = Container() var value = data[5] // also data[5] = 5 Also I want to be able to write something like this: data[1][2] Is it possible considering that element 1 from Container is an array ? Thanx for help. 回答1: It looks

Swift operator `subscript` []

自闭症网瘾萝莉.ら 提交于 2019-12-03 04:52:59
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 like this: var data: Container = Container() var value = data[5] // also data[5] = 5 Also I want to be able to write something like this: data[1][2] Is it possible considering that element 1 from Container is an array ? Thanx for help. It looks like there are 2 questions here. 1. How can I enable subscripting on my own custom class? To enable

Stack overflow when defining subscript on CKRecord in Swift

我是研究僧i 提交于 2019-12-01 17:53:58
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 { return objectForKey(key) as CKRecordValue? } set { setObject(newValue, forKey: key) } } The stack overflow occurs in the getter. (I've never tried the setter, so it may occur there, too.) I've tried implementing with objectForKey: , objectForKeyedSubscript: , and valueForKey: . All produce the same result: a stack overflow. This is very strange, since CKRecord is

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

限于喜欢 提交于 2019-12-01 03:35:11
This question already has an answer here: With arrays, why is it the case that a[5] == 5[a]? 18 answers Why does x[y] == y[x] in c++? [duplicate] 3 answers 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] = '5'; } Is it just because: a[b] is implemented as a macro *(a + b) by default and the fact that both code samples are valid is just an accident/compiler specific it's standardized somewhere and the outcome of

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

寵の児 提交于 2019-12-01 01:04:40
问题 This question already has answers here : With arrays, why is it the case that a[5] == 5[a]? (18 answers) Why does x[y] == y[x] in c++? [duplicate] (3 answers) Closed 5 years ago . 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] = '5'; } Is it just because: a[b] is implemented as a macro *(a + b) by default and