2012-03-16 18 views
7

W Mr Kenny Kerr this column on zdefiniowany struct i typedef takiego:Jakie jest znaczenie tego typedef?

struct boolean_struct { int member; }; 
typedef int boolean_struct::* boolean_type; 

to jaki jest sens tego typedef?

Inną kwestią jest dotyczącą następujący kod:

operator boolean_type() const throw() 
{ 
    return Traits::invalid() != m_value ? &boolean_struct::member : nullptr; 
} 

Jaki jest sens "& boolean_struct :: członka"?

Odpowiedz

13

W tej kolumnie Kenny Kerr, zdefiniował struct i typedef jak tym:

struct boolean_struct { int member; };  
typedef int boolean_struct::* boolean_type;  

Potem jaki jest sens tego typedef?

The typedef tworzy rodzaj zwany boolean_type który jest równoważny do wskaźnika do elementu wewnątrz boolean_struct obiektu int.

To jest nie to samo dla wskaźnika do int. Różnica polega na tym, że obiekt boolean_type wymaga obiektu boolean_struct w celu usunięcia go. Normalny wskaźnik do int nie. Najlepszym sposobem sprawdzenia, jak to jest inaczej, są przykłady kodu.

Rozważmy tylko normalne wskaźniki do int s:

struct boolean_struct { int member; }; 

int main() 
{ 
    // Two boolean_struct objects called bs1 and bs2 respectively: 
    boolean_struct bs1; 
    boolean_struct bs2; 
    // Initialize each to have a unique value for member: 
    bs1.member = 7; 
    bs2.member = 14; 

    // Obtaining a pointer to an int, which happens to be inside a boolean_struct: 
    int* pi1 = &(bs1.member); 
    // I can dereference it simply like this: 
    int value1 = *pi1; 
    // value1 now has value 7. 

    // Obtaining another pointer to an int, which happens to be inside 
    // another boolean_struct: 
    int* pi2 = &(bs2.member); 
    // Again, I can dereference it simply like this: 
    int value2 = *pi2; 
    // value2 now has value 14. 

    return 0; 
} 

teraz rozważyć, czy użyliśmy wskaźników do int członków Wewnątrz boolean_struct:

struct boolean_struct { int member; }; 
typedef int boolean_struct::* boolean_type; 

int main() 
{ 

    // Two boolean_struct objects called bs1 and bs2 respectively: 
    boolean_struct bs1; 
    boolean_struct bs2; 
    // Initialize each to have a unique value for member: 
    bs1.member = 7; 
    bs2.member = 14; 

    // Obtaining a pointer to an int member inside a boolean_struct 
    boolean_type pibs = &boolean_struct::member; 

    // Note that in order to dereference it I need a boolean_struct object (bs1): 
    int value3 = bs1.*pibs; 
    // value3 now has value 7. 

    // I can use the same pibs variable to get the value of member from a 
    // different boolean_struct (bs2): 
    int value4 = bs2.*pibs; 
    // value4 now has value 14. 

    return 0; 
} 

Jak widać, składnia i ich zachowania są różne.

Inną kwestią jest dotyczącą następujący kod:

operator boolean_type() const throw() 
{  
    return Traits::invalid() != m_value ? &boolean_struct::member : nullptr; 
} 

Jaki jest sens "& boolean_struct :: członka"?

Powoduje zwrócenie adresu zmiennej member wewnątrz boolean_struct. Zobacz powyższy przykład kodu.

+0

Dzięki za odpowiedź. Ale nie ma obiektu o nazwie boolean_struct, więc co dokładnie wskazuje element & boolean_struct :: member? – Rong

+1

@Rong: Wskazuje szczegóły implementacji kompilatora. Zwykle jest zaimplementowany jako pewnego rodzaju przesunięcie całkowite do elementu klasy. Ale jego dokładna wartość liczbowa jest nieistotna; co jest ważne, że pozwala na dostęp do określonej części obiektu określonego typu, bez względu na konkretną instancję tego typu. To tak, jakbym wskazywał na kończynę na ludzkim ciele; Mogę to zrobić bez względu na osobę. Właśnie dlatego działa wyrażenie 'boolean_struct :: member'; Mogę wskazać 'member' wewnątrz' boolean_struct' bez względu na konkretną instancję. –

Powiązane problemy