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: 
Rather than a collection of constants, you could have a constant collection. The property of being constant seems to pertain to your use case, not the data model itself. Like so:

```
struct extent { int width; int height; };

const extent e { 20, 30 };

```

It's possible to have specifically constant data members of a class, but then you need to write a constructor to initialize it:

```
struct Foo
{
    const int x;
    int & y;
    int z;

    Foo(int a, int & b) : x(a + b), y(b), z(b - a) {  }
};

```

(The example also shows another type of data member that needs to be initialized: references.)

Of course, structs and classes are the same thing.
You can initialize class const members with constructor. If you need add some other logic in constructor, but in .cpp file not in .h, you can create a private method and call it in constructor. 

**File.h**

```
    class Example 
    {
    private:
        const int constantMember1;
        const int constantMember2;
        const int constantMember3;
        void Init();
    public:
        Example(int a, int b) :constantMember1(a), constantMember2(b), constantMember3(a + b) {
           //Initialization
           Init();
        };
    };

```

**File.cpp**

```
void Init()
{
//Some Logic intialization
}

```