C++ Quiz no.3

Write minimal implementation of A and B such that the following expression to be legal:

A x;
B y;
y = & ( x + 1);

————————————

Answer to C++ Quiz no.2

#include <iostream>
using std::cout;

struct Row
{
 int a[3];
 Row(int x) { a[0] = x; a[1] = ++x; a[2] = ++x; }
 int * begin() { return a; } // a == &a[0]
 int * end() { return a + 3; } // a + 3 == &a[3]
};

struct Matrix
{
 Row r[3] = { 1, 4, 7 };
 Row * begin() { return r; }
 Row * end() { return r + 3; }
};

int main()
{
 Matrix m;
 for ( auto r : m ) //in pre-C++11 == for ( Row * r = m.begin(); r!= m.end(); ++r) // inside the body we need to use *r which is type Row
 {
  for ( auto e : r )
    cout << ' ' << e;
  cout << '\n';
 }
}
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s