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

后端 未结 17 833
傲寒
傲寒 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:10

    Having written numerous parsers, this seems like a dead simple list look up for the named object, either static or dynamic. Further, I have never seen a situation where the compiler did not correctly identify the missing object and type, so all those lame excuses for not allowing a WITH ...ENDWITH construction would seem to be a lot of hooey. For the rest of us prone to long object names one workaround is to create simple defines. Couldn't resist, suppose I have:

        #include<something> 
        typedef int headache;
        class grits{
          public:
           void corn(void);
           void cattle(void);
           void hay(void);}; //insert function defs here
         void grits::grits(void)(printf("Welcome to Farm-o-mania 2012\n");};
    
        #define m mylittlepiggy_from_under_the_backporch.
        headache main(){
           grits mylittlepiggy_from_under_the_backporch;
             m corn();  //works in GCC
             m cattle();
             m hay();
          return headache;
    
    0 讨论(0)
  • 2020-12-06 05:14

    A simple way to do this is as follows

    class MyClass
    {
        int& m_x;
    
        public MyClass(int& x)
        {
            m_x = x;
            m_x++;
        }
    
        ~MyClass()
        {
            m_x--;
        }
    }
    int main():
    {
        x = 0;
        {
            MyClass(x)  // x == 1 whilst in this scope
        }
    }
    

    I've been writing python all day long and just scrapped this down before anyone takes me to the cleaners. In a larger program this is an example of how to keep a reliable count for something.

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

    no there is no such keyword.

    0 讨论(0)
  • 2020-12-06 05:15
    #include <iostream>
    
    using namespace std;
    
    template <typename T>
    struct with_iter {
      with_iter( T &val ) : p(&val) {}
    
      inline T* begin() { return p; }
      inline T* end() { return p+1; }
    
      T *p;
    };
    
    #define with( N, I ) for( auto &N : with_iter<decltype(I)>(I) )
    
    int main() {
    
      with( out , cout ) {
        out << "Hello world!" << endl;
      }
    
      return 0;
    }
    

    Nuf said ...

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

    Even though I program mostly in Delphi which has a with keyword (since Delphi is a Pascal derivative), I don't use with. As others have said: it saves a bit on typing, but reading is made harder.

    In a case like the code below it might be tempting to use with:

    cxGrid.DBTableView.ViewData.Records.FieldByName('foo').Value = 1;
    cxGrid.DBTableView.ViewData.Records.FieldByName('bar').Value = 2;
    cxGrid.DBTableView.ViewData.Records.FieldByName('baz').Value = 3;
    

    Using with this looks like this

    with cxGrid.DBTableView.ViewData.Records do
    begin
      FieldByName('foo').Value = 1;
      FieldByName('bar').Value = 2;
      FieldByName('baz').Value = 3;
    end;
    

    I prefer to use a different technique by introducing an extra variable pointing to the same thing with would be pointing to. Like this:

    var lRecords: TDataSet;
    
    lRecords := cxGrid.DBTableView.ViewData.Records;
    
    lRecords.FieldByName('foo').Value = 1;
    lRecords.FieldByName('bar').Value = 2;
    lRecords.FieldByName('baz').Value = 3;
    

    This way there is no ambiguity, you save a bit on typing and the intent of the code is clearer than using with

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

    In C++, you can put code in a method of the class being reference by pointer. There you can directly reference the members without using the pointer. Make it inline and you pretty much get what you want.

    0 讨论(0)
提交回复
热议问题