2

Is it possible to create a class within another class? Is treating my class declaration as a member function. Here is my code:

struct Handler{
  int *_value = nullptr;
  Handler(int& value)
  {
    _value = &value;
  }
};
struct action{
  int _value = 0;
  Handler handler(_value);
};

So I want the _value in the struct Handler have the same adress with the _value in struct action, so when I change one of them, the other one will have the same value.

songyuanyao
  • 169,198
  • 16
  • 310
  • 405
Eden Cheung
  • 303
  • 2
  • 8

1 Answers1

2

Default member initializers only support brace or equals initializer, but not parentheses initializer. So you can write it as

Handler handler{_value};           // list initialization (since C++11)

or

Handler handler = Handler(_value); // copy initialization
songyuanyao
  • 169,198
  • 16
  • 310
  • 405