简体   繁体   English

这个typedef是什么意思?

[英]What is the meaning of this typedef?

In Mr Kenny Kerr's this column , he defined a struct and a typedef like this: 在Kenny Kerr先生的这一专栏中 ,他定义了一个结构和一个类似于这样的typedef:

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

Then what is the meaning of this typedef? 那这个typedef是什么意思?

Another question is concerning the following code: 另一个问题是关于以下代码:

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

What is the meaning of "&boolean_struct::member" ? “&boolean_struct :: member”是什么意思?

In Mr Kenny Kerr's this column, he defined a struct and a typedef like this: 在Kenny Kerr先生的这一专栏中,他定义了一个结构和一个类似于这样的typedef:

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

Then what is the meaning of this typedef? 那这个typedef是什么意思?

The typedef creates a type called boolean_type which is equivalent to a pointer to an int member inside a boolean_struct object. typedef创建一个名为boolean_type的类型,它相当于boolean_struct对象中指向int成员的指针。

It's not the same thing to a pointer to an int . 指向int的指针不是一回事。 The difference is that an object of boolean_type requires a boolean_struct object in order to dereference it. 所不同的是的目的boolean_type需要boolean_struct以便对象取消引用它。 A normal pointer to an int does not. 指向int普通指针不会。 The best way to see how this is different is via some code examples. 查看其不同之处的最佳方法是通过一些代码示例。

Consider only normal pointers to int s: 只考虑int的正常指针:

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;
}

Now consider if we used pointers to int members inside a boolean_struct : 现在考虑我们是否在boolean_struct使用指向int成员的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;
} 

As you can see, the syntax and their behavior are different. 如您所见,语法及其行为是不同的。

Another question is concerning the following code: 另一个问题是关于以下代码:

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

What is the meaning of "&boolean_struct::member" ? “&boolean_struct :: member”是什么意思?

This returns the address of the member variable inside a boolean_struct . 这将返回boolean_structmember变量的地址。 See above code example. 见上面的代码示例。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM