Home > Back-end >  C for (auto & x: TMP) traversal data in custom class
C for (auto & x: TMP) traversal data in custom class

Time:09-27

C + + for (auto & amp; X: TMP) traverses the custom class (class data chain table), how to avoid x.n ext - & gt; Data can be changed, please bosses to see what this code to improve

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 repeated

CodePudding user response:

I think now that encapsulates the linked list, next is set to private good, the outside world not just change

CodePudding 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:

refer to the second floor akari10032 response:
I think now that encapsulates the linked list, next is set to private good, the outside world may not be able to get no change to the

No, next set to private, that in class may not be able to access, this list can't chain up??

CodePudding user response:

I still write an abstract class, there is a const T& GetElement () of pure virtual function, structure inheritance this abstract class, implement this function retrieves a reference to the data, and invoke it with the operator *, so that in the for (auto & amp; X: TMP), x is the structure of the data of reference, rather than the structure of the reference

CodePudding user response:

reference Italink reply: 3/f
set variables 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)?

I think can traverse the list each structure, but don't want to let it can be changed, because the return is a const reference, so the return of the structure can not change, but because of containing the next, so you can borrow the next the structure behind the change
  • Related