How can I emulate destructuring in C++?

后端 未结 5 1247
轻奢々
轻奢々 2020-12-29 01:44

In JavaScript ES6, there is a language feature known as destructuring. It exists across many other languages as well.

In JavaScript ES6, it looks like this:

5条回答
  •  Happy的楠姐
    2020-12-29 02:06

    I am afraid you cannot have it the way you are used to in JavaScript (which by the way seems to be new technology in JS). The reason is that in C++ you simply cannot assign to multiple variables within a structure/object/assignment expression as you did in

    var {species, sound} = animal
    

    and then use species and sound as simple variables. Currently C++ simply does not have that feature.

    You could assign to structures and/or objects whilst overloading their assignment operator, but I don't see a way how you could emulate that exact behaviour (as of today). Consider the other answers offering similar solutions; maybe that works for your requirement.

提交回复
热议问题