Incomplete types question

via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Tue Jul 8 05:04:39 PDT 2014


On Tuesday, 8 July 2014 at 11:42:54 UTC, NoUseForAName wrote:
> On Tuesday, 8 July 2014 at 11:16:52 UTC, Marc Schütz wrote:
>> Your example above doesn't work, because it would be 
>> interpreted as a redefinition of the struct. Instead, you can 
>> use a public alias to a private type:
>>
>> // my_module.d:
>>
>> private struct MyStructImpl {
>>    int x;
>> }
>>
>> public alias MyStructPtr = MyStructImpl*;
>>
>> void doSomething(MyStructPtr foo) {
>>    ...
>> }
>>
>> // my_main_program.d:
>>
>> MyStructPtr bar;    // OK
>> MyStructImpl foo;   // Error: my_module.MyStructImpl is private
>
> Thanks, that is what I was looking for.

Addendum: You also need to protect the members of MyStructImpl, 
like so:

private struct MyStructImpl {
private:  // or `package:`
     int x;
}

Otherwise, they would still be accessible via dereferencing. 
(Don't worry, you _can_ access private members inside the module 
the struct is declared in, as access protection in D only applies 
across module boundaries.)


More information about the Digitalmars-d-learn mailing list