Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- using namespace std;
- template <typename T>
- struct Iter {
- Iter() { cout << '+' << this << endl; }
- ~Iter() { cout << '-' << this << endl; }
- T ** pp {nullptr};
- int len {0};
- int it {0};
- void set(T ** pi, int l) {
- pp = pi;
- len = l;
- }
- Iter & begin() {
- it = 0;
- return *this;
- }
- int end() const {
- return len;
- }
- T & operator*() {
- return *pp[it];
- }
- bool operator!=(int rhs) {
- return this->it < rhs;
- }
- Iter & operator++() {
- ++it;
- return *this;
- }
- };
- class A {
- public:
- A() : _len(4) {
- _ori_aa = new int [_len];
- _aa = new int * [_len];
- for(int i = 0; i < _len; i++) {
- _ori_aa[i] = i;
- _aa[i] = _ori_aa + i;
- }
- }
- ~A() {
- delete [] _aa;
- delete [] _ori_aa;
- }
- Iter<int> aa() const {
- Iter<int> _iter;
- _iter.set(_aa, _len);
- return _iter;
- }
- private:
- const int _len;
- int * _ori_aa;
- int ** _aa;
- };
- int main() {
- A a;
- Iter<int> aa = a.aa(); // copy by value
- cout << '?' << &aa << endl;
- for(const int & n : aa) {
- cout << n << endl;
- }
- cout << "-------" << endl;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement