Question: I'm coming to C++ from C# and const-correctness is still new to me. In C# I could declare a property like this:

```
class Type 
{
    public readonly int x;
    public Type(int y) 
    {
        x = y;
    }
}

```

This would ensure that x was only set during initialization. I would like to do something similar in C++. The best I can come up with though is:

```
class Type
{
private:
    int _x;
public:
    Type(int y) { _x = y; }
    int get_x() { return _x; }
};

```

Is there a better way to do this? Even better: Can I do this with a struct? The type I have in mind is really just a collection of data, with no logic, so a struct would be better if I could guarantee that its values are set only during initialization.

Answer: 
There is a `const` modifier:

```
class Type
{
private:
   const int _x;
   int j;

public:
    Type(int y):_x(y) { j = 5; }
    int get_x() { return _x; }
    // disable changing the object through assignment
    Type& operator=(const Type&) = delete;
};

```

Note that you need to initialize constant in the constructor initialization list. Other variables you can also initialize in the constructor body.

About your second question, yes, you can do something like this:

```
   struct Type
   {
      const int x; 
      const int y;

      Type(int vx, int vy): x(vx), y(vy){}
      // disable changing the object through assignment
      Type& operator=(const Type&) = delete;
   };

```
This is not exactly answering the question asked, but if you wanted to have the simplicity of directly accessing member variables in a struct without getters, but wanted to ensure that nobody could modify the values, you could do something like this:

```
#include <iostream>

using namespace std;

class TypeFriend;

struct Type
{
  const int &x;
  const int y;

  Type (int vx, int vy):x (_x), y (vy), _x (vx)
  {
  }

private:
  friend class TypeFriend;
  int _x;

};

struct TypeFriend
{
  TypeFriend (Type & t):_t (t)
  {
  }

  void setX (int newX)
  {
    _t._x = newX;
  }

private:
  Type & _t;
};

int main ()
{
  Type t (1, 2);
  TypeFriend tf (t);

  cout << t.x << "," << t.y << endl;

  // t.x = 6;  // error: assignment of read-only location ‘t.Type::x’
  // cout<<t.x << ","<<t.y<<endl;

  tf.setX (5);
  cout << t.x << "," << t.y << endl;

  return 0;
}

```

The result of running this is:

```
1,2
5,2

```

Type::x cannot be modified externally, so it is read-only, but via TypeFriend it can be changed. This can be useful if you wanted to expose a simple interface of direct member access for reading, but wanted to restrict how those members could be changed.