Does C++ have “with” keyword like Pascal?

后端 未结 17 852
傲寒
傲寒 2020-12-06 05:02

with keyword in Pascal can be use to quick access the field of a record. Anybody knows if C++ has anything similar to that?

Ex: I have a pointer with m

17条回答
  •  借酒劲吻你
    2020-12-06 05:24

    I can see one instance where 'with' is actually useful.

    In methods for recursive data structures, you often have the case:

    void A::method()
    {
      for (A* node = this; node; node = node->next) {
        abc(node->value1);
        def(value2); // -- oops should have been node->value2
        xyz(node->value3);
      }
    }
    

    errors caused by typos like this are very hard to find.

    With 'with' you could write

    void A::method()
    {
      for (A* node = this; node; node = node->next) with (node) {
        abc(value1);
        def(value2);
        xyz(value3);
      }
    }
    

    This probably doesn't outweight all the other negatives mentioned for 'with', but just as an interesting info...

提交回复
热议问题