A fake c + +, feel learn at school sophomore did not even understand it language, yesterday saw a c + + 11 features (realized with c + + version), found that c + + for also can use, like c # foreach so he wrote a class like IEnumerator interface in c #, then write their own class can easily use the for (auto & amp; X: TMP) to traverse the data
But there is a problem now is that the operator * is the return type of const T& So x.d ata and x.n ext can't change, but x.n ext - & gt; The data can change
Now I can only think of adding a GetData function to structure, or add a read-only Data property returns the Data references (equivalent to let structure implements an interface), inside the operator * call, but I still want to have to do is just inherited IEnumerator, so wanted to ask bosses have good solution
IEnumerator three hands: start represents the first element, the last on behalf of the last element at the back of the elements (nullptr), next represent a return to the next element function
CodePudding user response:
1 and 2 pictures like card out two more, zhang and third four repeatedCodePudding user response:
I think now that encapsulates the linked list, next is set to private good, the outside world not just changeCodePudding user response:
The variable is set to private, by increasing the set, the get method to deal with good,'operator * return type is const T& So x.d ata and x.n ext can't change, but x.n ext - & gt; Data can change '
If you don't pass function to deal with, if you want to change, how do you get?
You mean now, will have direct access to it, but just want to visit and cannot be modified, is that possible?
Or do you want to ask for (const Type & amp; It: containter)?
CodePudding user response: