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

后端 未结 17 832
傲寒
傲寒 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:19
    with (OBJECT) {CODE}
    

    There is no such thing in C++.
    You can put CODE as is into a method of OBJECT, but it is not always desirable.

    With C++11 you can get quite close by creating alias with short name for OBJECT.
    For example code given in question it will look like so:

    {
        auto &_ = *pointer;
        if (_.field1 && ... && _.fieldn) {...}
    }
    

    (The surrounding curly braces are used to limit visibility of alias _ )

    If you use some field very often you can alias it directly:

    auto &field = pointer->field;
    // Even shorter alias:
    auto &_ = pointer->busy_field;
    
    0 讨论(0)
  • 2020-12-06 05:19

    Maybe you can:

    auto p = *pointer;
    if (p.field1) && (p.field2) && ... (p.fieldn)
    

    Or create a small program that will understand with statements in C++ and translate them to some form of a valid C++.

    0 讨论(0)
  • 2020-12-06 05:21

    The closest you can get is method chaining:

    myObj->setX(x)
         ->setY(y)
         ->setZ(z)
    

    for setting multiple fields and using for namespaces.

    0 讨论(0)
  • 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...

    0 讨论(0)
  • 2020-12-06 05:28

    Probably the closest you can get is this: (Please don't downvote me; this is just an academic exercise. Of course, you can't use any local variables in the body of these artificial with blocks!)

    struct Bar {
        int field;
    };
    
    void foo( Bar &b ) {
        struct withbar : Bar { void operator()() {
            cerr << field << endl;
        }}; static_cast<withbar&>(b)();
    }
    

    Or, a bit more demonically,

    #define WITH(T) do { struct WITH : T { void operator()() {
    #define ENDWITH(X) }}; static_cast<WITH&>((X))(); } while(0)
    
    struct Bar {
        int field;
    };
    
    void foo( Bar &b ) {
        if ( 1+1 == 2 )
            WITH( Bar )
                cerr << field << endl;
            ENDWITH( b );
    }
    

    or in C++0x

    #define WITH(X) do { auto P = &X; \
     struct WITH : typename decay< decltype(X) >::type { void operator()() {
    #define ENDWITH }}; static_cast<WITH&>((*P))(); } while(0)
    
            WITH( b )
                cerr << field << endl;
            ENDWITH;
    
    0 讨论(0)
提交回复
热议问题