storing static data of variable types

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Gernot Frisch

    storing static data of variable types

    Hi,

    I've got to write a BASIC to C++ converter and came across the problem of
    the DATA statement.

    I must provide a way of storing:
    DATA 5.123, "abc", ...
    in C++.

    my first guess was this:

    struct
    {
    double* pD;
    const char* pC;
    } str[2]={
    {NULL, "xy"},
    {&123.4, NULL},
    };


    but &123.4 is not possible. Now, having temporary global variables for this
    doesn't make me too happy either.

    Is there a "clean" way of doing this?


    --
    ------------------------------------
    Gernot Frisch
    GLBasic is a programming language that supports multiple platforms like e.g. iPhone


  • Ian Collins

    #2
    Re: storing static data of variable types

    Gernot Frisch wrote:
    Hi,
    >
    I've got to write a BASIC to C++ converter and came across the problem
    of the DATA statement.
    >
    I must provide a way of storing:
    DATA 5.123, "abc", ...
    in C++.
    >
    my first guess was this:
    >
    struct
    {
    double* pD;
    const char* pC;
    } str[2]={
    {NULL, "xy"},
    {&123.4, NULL},
    };
    >
    >
    but &123.4 is not possible. Now, having temporary global variables for
    this doesn't make me too happy either.
    >
    Is there a "clean" way of doing this?
    >
    Store the values as strings and convert then back as required?

    --
    Ian Collins.

    Comment

    • Michael DOUBEZ

      #3
      Re: storing static data of variable types

      Gernot Frisch a écrit :
      Hi,
      >
      I've got to write a BASIC to C++ converter and came across the problem
      of the DATA statement.
      >
      I must provide a way of storing:
      DATA 5.123, "abc", ...
      in C++.
      >
      my first guess was this:
      >
      struct
      {
      double* pD;
      const char* pC;
      } str[2]={
      {NULL, "xy"},
      {&123.4, NULL},
      };
      >
      >
      but &123.4 is not possible. Now, having temporary global variables for
      this doesn't make me too happy either.
      >
      Is there a "clean" way of doing this?
      Directly stores the value instead:

      struct
      {
      double pD;
      const char* pC;
      }str[2]={
      {NULL, "xy"},
      {123.4, NULL},
      };

      Michael

      Comment

      • Gernot Frisch

        #4
        Re: storing static data of variable types


        Directly stores the value instead:
        >
        struct
        {
        double pD;
        const char* pC;
        }str[2]={
        {NULL, "xy"},
        {123.4, NULL},
        };

        First I thought I'd store twice the ammount hten. But thinking of it, my way
        I'd store the number _plus_ a pointer to it. I'm so stupid.
        Thank you for showing me :)

        Comment

        • Michael DOUBEZ

          #5
          Re: storing static data of variable types

          Gernot Frisch a écrit :
          >
          >
          >Directly stores the value instead:
          >>
          >struct
          >{
          > double pD;
          > const char* pC;
          >}str[2]={
          > {NULL, "xy"},
          > {123.4, NULL},
          > };
          >
          >
          First I thought I'd store twice the ammount hten. But thinking of it, my
          way I'd store the number _plus_ a pointer to it. I'm so stupid.
          Thank you for showing me :)
          >
          If memory space is an issue, you could use a union (and a type tag to
          retrieve the relevant information).

          It is common in many interpreters written in C:
          struct
          {
          int type; //give the type of data
          union
          {
          const char* m_string;
          double m_double;
          long m_integer;
          //...
          } data;
          }

          Nowadays, I think there is Boost.Variant that simplifies this much but I
          have no experience with it. Perhaps worth a look.

          Michael

          Comment

          • Gernot Frisch

            #6
            Re: storing static data of variable types


            DATA 123.4, "xy" translates to:
            >
            static Data data[] = { Data( 123.4 ), Data( "xy" ) /* , ... */ };
            Repository MyData( &data[0], &data[sizeof(data) / sizeof(Data)] );

            I did something similar already. Thank you for your time and help.
            For the Read, I just overloaded 2 functions:
            void READ(MyNumber& dbl);
            void READ(MyString& str);



            Comment

            Working...