473,327 Members | 2,118 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,327 software developers and data experts.

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
http://www.glbasic.com

Mar 11 '08 #1
5 1453
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.
Mar 11 '08 #2
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
Mar 11 '08 #3

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 :)

Mar 11 '08 #4
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
Mar 11 '08 #5

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);

Mar 12 '08 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: bissatch | last post by:
Hi, I am currently writing a news admin system. I would like to add the ability to add images to each article. What I have always done in the past is uploaded (using a form) the image to a...
10
by: Peter Kirk | last post by:
Hi there I have a question about static variables. There is a function which returns a pointer to a structure "AStructure". The function has a static local variable of type AStructure, and it is...
12
by: Joe Narissi | last post by:
I know how to create and use static constructors, but is there a such thing as a static destructor? If not, then how do you deallocate memory intialized in the static constructor? Thanks in...
2
by: newjazzharmony | last post by:
All, I'd appreciate it if someone could answer these questions for me. This scenario applies to an ASP dot net application that is deployed in a web farm. 1) Does the application live in a...
18
by: Jack | last post by:
Thanks.
1
by: None | last post by:
Hi, I have developed webshop application using asp.net 1.1. I'm using DataGrid in one of the pages of my site. During the page load the DataGrid will be binded by around 7500 products(rows). At...
3
by: Steve Folly | last post by:
Hi, I had a problem in my code recently which turned out to be the 'the "static initialization order fiasco"' problem (<http://www.parashift.com/c++-faq-lite/ctors.html#faq-10.12>) The FAQ...
9
by: LamSoft | last post by:
Class B { public B() {} } Class A : B { public static string ABC = "myABC"; public A() {} }
15
by: akomiakov | last post by:
Is there a technical reason why one can't initialize a cost static non- integral data member in a class?
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.