Overloading struct members (or cast?)
    Ali Çehreli via Digitalmars-d-learn 
    digitalmars-d-learn at puremagic.com
       
    Tue Nov 18 10:28:59 PST 2014
    
    
  
On 11/18/2014 07:32 AM, Wsdes wrote:
 >             Error: need 'this' for 'string' of type 'const(char*)'
Reduced:
struct S
{
     union Value
     {
         const char* myString;
     }
}
void main()
{
     auto s = S();
     auto mine = s.Value.myString;    // ERROR
}
You may be thinking that Value introduces a name scope so that you can 
reference its myString by Value.myString.
You have two options:
a) Remove Value so that it is an anonymous union. myString becomes a 
direct member of S:
struct S
{
     union    // <-- Anonymous
     {
         const char* myString;
     }
}
void main()
{
     auto s = S();
     auto mine = s.myString;    // <-- No more Value
}
b) Make S have an actual member of type Value:
struct S
{
     union Value
     {
         const char* myString;
     }
     Value value;    // <-- Added
}
void main()
{
     auto s = S();
     auto mine = s.value.myString;    // <-- Note lower initial
}
Ali
    
    
More information about the Digitalmars-d-learn
mailing list