From a member of a nested list, how can I call external data

struct object {
      string sent ;
      ...// other data declaration
      struct nested {
            void read ( void ) ;
      };
};

In the read function, how can I fill out the sent one? In other words, how can I call a sender

EDIT:

I know this is a trivial question, but I don’t know so much about the nested structure, and you can give any recommendation on the website

+3
source share
2 answers

A nested class just needs a pointer or a reference to the surrounding class. This can be passed through the built-in class constructor.

struct nested 
{
  nested(object& obj) : m_obj(obj) { }

  object& m_obj;
};

Then you can access object::sentusing the reference variable m_obj.

+5
source

You need to make some changes to your structure:

struct object {
      std::string sent ;

      struct nested {
            void read ( object& obj ) { obj.sent = "FOO"; }
      } bar;
};

-, read , nested object ( , , ), object ..

object foo;
foo.bar.read(foo); // this will set it

EDIT: nested , object, ctor object, bar (*this), , , nested " ".

+2

Source: https://habr.com/ru/post/1788135/


All Articles