#include <iostream>
#include <memory>
#include <stdexcept>
using namespace std;
 
class S
{
private:
	int* data;
    bool is_const;
public:
    S() : data(new int[10]), is_const(false) { data[1] = 42; }
    S(const S& other) : data(other.data), is_const(true) {}
    S(S& other) : data(other.data), is_const(false) {}

    int& operator()(size_t i)
    {
    	if (is_const)
    		throw std::logic_error("non-const operation attempted");
        return data[i];
    }
 
    const int& operator()(size_t i) const
    {
        return data[i];
    }
};
 
int main() {
	try
	{
	    const S a; // Allocates memory
    	const S c(a);
    	cout << c(1) << endl;
    	S b(a); // Also points to memory allocated for a
    	b(1) = 3; // Generates an exception
    	cout << b(1) << endl; // never reached
	}
	catch (exception &e)
	{
		cout << "exception: " << e.what() << endl;
	}
	return 0;
}