疯狂的C++ template - 用于访问类的各个属性的模板
我是一个新手 C++ 程序员,但我以为我对 C++ 足够了解,直到今天我在工作中遇到这样的代码,但未能理解它实际上是如何工作的。
class Object
{
};
template <
class PropObject,
class PropType,
PropType PropObject::* Prop
>
class PropReader
{
public:
void print(Object& o)
{
PropObject& po = static_cast<PropObject &>(o);
PropType& t = po.*Prop;
cout << t << "\n";
}
};
class Student : public Object
{
public:
int age;
int grade;
};
int _tmain(int argc, _TCHAR* argv[])
{
Student s;
s.age = 10;
s.grade = 5;
PropReader<Student, int, &Student::age> r;
PropReader<Student, int, &Student::grade> r2;
r.print(s);
r2.print(s);
}
我想我已经在较高的层面上理解了。但模板声明中这个特定的 PropType PropObject::* Prop
让我很困扰。这是什么意思?我正在寻找 C++ 专家的解释。我想了解它,以便我可以更好地使用它。不过它看起来非常有用。
I am a novice C++ programmer, but I thought I know enough about C++ until today when I came across code like this at work and failed to understand how it actually works.
class Object
{
};
template <
class PropObject,
class PropType,
PropType PropObject::* Prop
>
class PropReader
{
public:
void print(Object& o)
{
PropObject& po = static_cast<PropObject &>(o);
PropType& t = po.*Prop;
cout << t << "\n";
}
};
class Student : public Object
{
public:
int age;
int grade;
};
int _tmain(int argc, _TCHAR* argv[])
{
Student s;
s.age = 10;
s.grade = 5;
PropReader<Student, int, &Student::age> r;
PropReader<Student, int, &Student::grade> r2;
r.print(s);
r2.print(s);
}
I think I kind of understood at a high level. But this particular PropType PropObject::* Prop
in the template declaration bothers me. What does it mean? I am looking for an explanation from C++ experts. I would like to understand it, so that I can use it better. It looks very useful though.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
C++ 模板以将类型作为参数而闻名,但它们也可以对其他类型的数据进行参数化。例如,您可以通过整数对类进行模板化,如下所示:
模板也可以通过指针进行参数化,只要指针满足特定条件(例如,它必须计算出可以在以下位置确定的内容的地址):编译时)。例如,这是完全合法的:
在您的代码中,模板通过类成员的指针进行参数化。指向类成员的指针与指针类似,因为它间接引用某个对象。但是,它不是指向对象,而是指向类中的字段。这个想法是,您可以取消引用相对于某个对象的类成员指针,以从类中选择该字段。下面是指向类成员的指针的一个简单示例:
请注意,声明指向类成员的指针的语法是
因此,在上面的代码中,
int MyStruct::* ptr
的意思是“a指向MyStruct
类内的int
的指针在您发布的代码中,模板声明如下所示:
让我们看看这是什么意思 前两个模板。其属性将要变化的参数对象要读取的属性,以及
PropType
,该属性的类型。”模板的最后一个参数是一个名为Prop
的类成员指针,它指向PropObject
内部PropType
类型的字段。例如,您可以使用MyStruct
实例化此模板,如下所示:现在,让我们看看其余代码的作用。该类模板的正文重印于此:
其中一些内容可以很容易阅读。该函数的参数是对名为
o
的Object
的引用,最后一行打印出一些字段。这两行很棘手:第一行是一个类型转换,表示“尝试将参数
o
转换为PropObject
类型的引用。我猜这个想法是,是Object
是许多不同对象的一些基类。函数的参数只是一个普通的Object
,并且此转换尝试将其转换为某种类型。适当的类型(回想一下PropObject
是模板参数说明对象的类型是什么),因为这使用了static_cast
,如果未定义转换(例如,您尝试通过int
实例化模板)。 > 或vector
),代码将无法编译,否则,代码会相信转换是安全的,然后获取PropObject
类型的引用。 参数指的最后最后一行
是 。使用我之前提到的指向类成员的指针取消引用语法来表示“选择
Prop
(模板参数)指向的字段,然后存储对它的引用,名为t.
简而言之,模板
print
函数,给定对象尝试打印该字段。哇!这很棘手!希望这有帮助!
C++ templates are well-known for taking types as arguments, but they can also be parameterized over other types of data as well. For example, you could templatize a class over an integer, as shown here:
Templates can also be parameterized over pointers, as long as the pointer meets certain criteria (for example, it has to evaluate to an address of something that can be determined at compile-time). For example, this is perfectly legal:
In your code, the template is parameterized over a pointer-to-class-member. A pointer-to-class-member is similar to a pointer in that it indirectly refers to some object. However, instead of pointing to an object, instead it points to a field in a class. The idea is that you can dereference a pointer-to-class-member relative to some object to select that field out of the class. Here's a simple example of pointers-to-class-member:
Notice that the syntax for declaring a pointer-to-class-member is
So in the above code,
int MyStruct::* ptr
means "a pointer to anint
inside of aMyStruct
class.In the code that you've posted, the template declaration reads like this:
Let's see what this means. The first two template argument object whose property is going to be read, and
PropType
, the type of that property." The final argument to the template is a pointer-to-class-member namedProp
that points inside aPropObject
at a field of typePropType
. For example, you could instantiate this template withMyStruct
like this:Now, let's see what the rest of the code does. The body of this class template is reprinted here:
Some of this can be read pretty easily. The parameter to this function is a reference to an
Object
namedo
, and the last line prints out some field. These two lines are tricky:This first line is a typecast that says "try to cast the argument
o
to a reference of typePropObject
. The idea, I'm guessing, is thatObject
is some base class of a lot of different objects. The parameter to the function is just a plainObject
, and this cast tries to convert it to something of the appropriate type (recall thatPropObject
is the template argument saying what the type of the object is). Because this usesstatic_cast
, if the conversion isn't defined (for example, you tried to instantiate the template overint
orvector<string>
), the code won't compile. Otherwise, the code trusts that the cast is safe, then gets a reference of typePropObject
to what the parameter refers to.Finally, the last line is
This uses the pointer-to-class-member dereference syntax I mentioned earlier to say "select the field pointed at by
Prop
(the template argument), then store a reference to it namedt
.So, in short, the template
print
function that given an object tries to print out that field.Whew! That was tricky! Hope this helps!
C 或 C++ 中的声明通常最好从右到左阅读:
这可以在实例化中看到:
这里第三个模板参数是指向
Student
类成员的指针,该类的类型为int
。Declarations in C or C++ are often best read from right to left:
This can be seen in action in the instantiations:
Here the third template parameter is a pointer to a member of the
Student
class, that has typeint
.PropObject::*
是一个指向成员(在本例中为数据成员)的指针。它基本上是一个指向(非静态)成员的指针(在您的代码中是Student::age
和Student::grade
)。要使用它,您必须指定该函数将使用的
this
对象。这是通过使用.*
或->*
运算符来完成的;在这种情况下,PropType& t = po.*Prop; 行处理该问题,其中po
用作成员的this
对象。The
PropObject::*
is a pointer-to-member (data member, in this case). It's basically a pointer to a (non-static) member (which areStudent::age
andStudent::grade
in the case of your code).To use it, you have to specify the
this
object that the function would use. That's done by using either the.*
or->*
operator; in this case, thePropType& t = po.*Prop;
line handles that, wherepo
is used as thethis
object for the members.这是将指针作为参数传递给成员的语法。具体来说,在本例中,可以传入
age
和grade
成员。虽然模板参数指定Student
类,但它是并且成员属性是int
。如果您向学生添加了字符串名称,则 PropReader 声明可能如下所示:
That's the syntax for passing a pointer to a member as an argument. Specifically in this case it's so that
age
andgrade
members can be passed in. While the template args are specifying theStudent
class it's a member of and that the member properties areint
.If you added a string name to student the PropReader declaration might look like this:
这定义了指向类的成员变量的指针,在本例中,类是 PropObject,变量类型是 PropType。
This defines pointer that pointing a member variable of a class, in this particular case, the class is PropObject and the variable type is PropType.