I have the following:
class MyClass
{
private:
int my_data;
MyClass* my_pointer;
public:
MyClass():
my_data(0),
my_pointer(this)
{}
MyClass(const int& data):
my_data(data),
my_pointer(this)
{}
};
However, when I run
std::vector<MyClass> vec(10);
int n = {0};
std::generate(vec.begin(), vec.end(), [&n] { return MyClass(n++); });
each of my_pointer for the ten instances has the same value. How can I correct this code so that my_pointer points to the instance the field is in?