delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1999/07/07/11:56:53

From: "Campbell, Rolf [SKY:1U32:EXCH]" <cp1v45 AT americasm01 DOT nt DOT com>
Newsgroups: comp.os.msdos.djgpp
Subject: Re: bison variable access
Date: Wed, 07 Jul 1999 10:57:16 -0400
Organization: Nortel Networks
Lines: 35
Message-ID: <37836ACB.52F5B117@americasm01.nt.com>
References: <199907020051 DOT UAA06732 AT indy DOT delorie DOT com> <377C404E DOT 36E22B7 AT hotmail DOT com> <378182F5 DOT AF4797B3 AT unb DOT ca> <3781BA61 DOT A5028CFA AT hotmail DOT com> <3782043A DOT 28A947D5 AT americasm01 DOT nt DOT com> <3782EEE5 DOT FDE29FE8 AT hotmail DOT com>
NNTP-Posting-Host: bmerhc00.ca.nortel.com
Mime-Version: 1.0
X-Mailer: Mozilla 4.6 [en] (X11; I; HP-UX B.10.20 9000/712)
X-Accept-Language: en
To: djgpp AT delorie DOT com
DJ-Gateway: from newsgroup comp.os.msdos.djgpp
Reply-To: djgpp AT delorie DOT com

Denis Lamarche wrote:

> > struct vars
> > {
> > char *vname;
> > double *var;
> > };
> >
> > struct vars variables[]
> > = {
> >   {"drive", &drive},
> >   {"room", &room},
> >   {"picture", &picture},
> >   {"house", &house}
> >   };
> >
> Ok...I see were I went wrong....ok, but would this work for structures
> too?  If I assign the pointer to that structure to *var, can I later go
> variables[0].var->x=10?  if I assign the structure pointer to var then
> it will turn into a double right?...how would I re-convert it int that
> structure without knowing what that structure is?

Now you're getting complicated.  The only way I can think of is to use some "Variant" data
type.  That would consist of a giant union of every possible datatype, and a
type-specifier.  Every time you wanted to make an assignment, or read a value, you would
have to go through a giant switch and choose the right union member.
    I can think of a neat way of doing it in C++ using a class and some overloaded
operators.  It would still require the union, but now all you would have to do is overload
the '=' operator for every datatype, and just make the assignment into that union member.

--
     -Rolf Campbell (39)3-6318



- Raw text -


  webmaster     delorie software   privacy  
  Copyright 2019   by DJ Delorie     Updated Jul 2019