简体   繁体   English

如何在C ++类模板中访问指向成员参数的指针?

[英]How to access pointer to member parameter in C++ class template?

I'm trying to create a C++ class template that takes advantage of a pointer to member parameter but compilation fails. 我正在尝试创建一个C ++类模板,该模板利用指向成员参数的指针,但是编译失败。 How do I access the pointer to member parameter from within the template? 如何从模板中访问指向成员参数的指针?

class Base {
public:
  int foo;
  int bar;
};

template<int Base::* T>
class Derived : public Base {
public:
  int Get() { return *T; }  <--- Does not work
};

Derived<&Base::foo> test;
printf("Value = %i\n", test.Get());

The compilation error from Clang is indirection requires pointer operand ('int Test::*' invalid) . 来自Clang的编译错误是indirection requires pointer operand ('int Test::*' invalid)

Try 尝试

int Get() { return this->*T; }

.* and ->* are the operators for dereferencing pointers-to-members. .*->*是用于取消引用成员指针的运算符。

return this->*T;

(为什么T ?这不是一种类型。)

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

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