C ++ 11; Can initialization of non-static data members refer to other data members?

I really like the idea of ​​properties in C #, and as a small side project I am working on the idea of ​​implementing them in C ++. I came across this example https://stackoverflow.com/a/312947/ which seems pretty nice, but I couldn’t help but think that initializing lambda elements and non-static data might allow very good syntax to be used with this idea. Here is my implementation:

#include <iostream>
#include <functional>

using namespace std;


template< typename T >
class property {

public:
    property(function<const T&(void)> getter, function<void(const T&)> setter)
        : getter_(getter),
          setter_(setter)
    {};

    operator const T&() {
        return getter_();
    };

    property<T>& operator=(const T& value) {
        setter_(value);
    }

private:
    function<const T&(void)> getter_;
    function<void(const T&)> setter_;

};


class Foobar {

public:
    property<int> num {
        [&]() { return num_; },
        [&](const int& value) { num_ = value; }
    };

private:
    int num_;

};


int main() {
    // This version works fine...
    int myNum;
    property<int> num = property<int>(
        [&]() { return myNum; },
        [&](const int& value) { myNum = value; }
    );
    num = 5;

    cout << num << endl;  // Outputs 5
    cout << myNum << endl;  // Outputs 5 again.

    // This is what I would like to see work, if the property
    // member of Foobar would compile...
    // Foobar foo;
    // foo.num = 5;

    // cout << foo.num << endl;

    return 0;
}

I can use my property class normally (see the example in main ()], but MinGW with g ++ 4.7 doesn’t particularly care about my attempt to use the property as a data element:

\property.cpp: In lambda function:
\property.cpp:40:7: error: invalid use of non-static data member 'Foobar::num_'

, , , , -. , , , , ?

+5
1

( property<int>) ( Foobar). , - this, , num_, . lambdas - Foobar, this (, this->num_). lambdas , . lambdas num_, num_, Foobar, :

, , - . , . , ( property<int, Foobar> num), , this. , , , ++ 11.

this lambdas ( , !), , Foobar ( ):

Foobar::Foobar():
    num {
        [this]() { return this->num_; },
        [this](const int& value) { this->num_ = value; }
    }
{
}

- , this, , ? , , , .

+2

All Articles