anonymous structs within structs
DLearner
bmqazwsx123 at gmail.com
Tue Dec 5 11:05:21 UTC 2023
On Tuesday, 5 December 2023 at 00:31:35 UTC, H. S. Teoh wrote:
> On Mon, Dec 04, 2023 at 11:46:45PM +0000, DLearner via
> Digitalmars-d-learn wrote: [...]
>> Basically, B corresponds to the whole record (and only a whole
>> record
>> can be read).
>> But the task only requires Var1 and Var2, the last two fields
>> on the record.
>> By putting all the irrelevant fields into A, and defining B as
>> above,
>> program remains unpolluted with data it does not need.
> [...]
>
> Sounds like what you need is something like this:
>
> struct Record {
> struct UnimportantStuff {
> ...
> }
> UnimportantStuff unimportant;
>
> struct ImportantStuff {
> ...
> }
> ImportantStuff important;
> }
>
> ImportantStuff readData() {
> Record rec = readData(...); // read entire record
> return rec.important; // discard unimportant stuff
> }
>
> int main() {
> ...
> ImportantStuff data = readData(); // only important stuff
> returned
> processData(data);
> ...
> }
>
>
> T
I think that what you propose would work.
However, what I was really interested in was finding a way (using
your example) of not instantiating UnimportantStuff anywhere.
Your suggestion instantiates it at:
```
UnimportantStuff unimportant;
```
I was thinking (again using your example) of something like:
```
struct UnimportantStuff {
...
}
struct Record {
UnimportantStuff.sizeof;
struct ImportantStuff {
...
}
ImportantStuff important;
}
```
But was concerned about possible alignment issues.
More information about the Digitalmars-d-learn
mailing list