指向方法和指针模板问题,C++
有 2 个模板类 A 和 B,有 2 个私有成员 a1、a2 和 b1、b2。
template <typename T>
class A
{
private:
T a1, a2;
public:
T getA1 () const {return a1;}
T getA2 () const {return a2;}
};
template <typename T>
class B
{
private:
T b1, b2;
public:
T getB1 () const {return b1;}
T getB2 () const {return b2;}
};
在 Test 类中,需要 2 个指向 getter 的指针。
class Test
{
private:
template <typename T>
static T ( *getFirst ) ();
template <typename T>
static T ( *getSecond ) ();
}
template <typename T>
T ( * Test::getFirst ) () = &A<T>::getA1; //Pointer to getA1, error
template <typename T>
T ( * Test::getSecond ) () = &B<T>::getB2; //Pointer to getB2, error
int main
{
A <double> a;
B <double> b;
double c = a.getFirst + b.getSecond;
}
T 代表基本数据类型...是否可以在没有专门化(即指向类模板成员的指针)的情况下实现此代码,或者那些“指针”应该专门化?感谢您提供任何示例...
There are 2 template classes A and B having 2 private members a1, a2 and b1, b2.
template <typename T>
class A
{
private:
T a1, a2;
public:
T getA1 () const {return a1;}
T getA2 () const {return a2;}
};
template <typename T>
class B
{
private:
T b1, b2;
public:
T getB1 () const {return b1;}
T getB2 () const {return b2;}
};
In the class Test there is a need for 2 pointers pointing to getters.
class Test
{
private:
template <typename T>
static T ( *getFirst ) ();
template <typename T>
static T ( *getSecond ) ();
}
template <typename T>
T ( * Test::getFirst ) () = &A<T>::getA1; //Pointer to getA1, error
template <typename T>
T ( * Test::getSecond ) () = &B<T>::getB2; //Pointer to getB2, error
int main
{
A <double> a;
B <double> b;
double c = a.getFirst + b.getSecond;
}
T represents fundamental data types... Is it possible implement this code without specialization (i.e. pointers to class template members) or those "pointers" should be specialized? Thanks for any examples...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
你正在做违法的事情。看到这个,
这里你试图定义模板函数指针,这在 C++ 中是非法的。
C++ 标准在 $14/1 中表示,
请注意,它不是说“模板定义了一系列类、函数或函数指针 >”。所以你想要做的是,使用模板定义“函数指针家族”,这是不允许的。
如果你想要函数指针,你可以这样做,
更好的选择是:使用函数对象。 :-)
You're doing illegal things. See this,
Here you're trying to define template function pointer which is illegal in C++.
The C++ Standard says in $14/1,
Please note that it does not say "a template defines a family of classes, functions or function pointers". So what you're trying to do is, defining "a family of function pointers" using template, which isn't allowed.
If you want function pointer you can do something like this,
Yet better alternative is : use function object. :-)
简而言之,这是不可能的。
首先,不能声明指向模板函数的指针,只能声明指向具体函数的指针。
其次,您尝试声明指向自由函数的指针,但
A::getA1
是带有隐式this
参数的成员函数,因此语义不匹配。您可以执行以下操作:
将指向模板函数的指针保留为模板类的成员
In short, it's not possible.
First, you cannot declare a pointer to template function, only pointer to a concrete function.
Second, you tried to declare pointer to free function but
A::getA1
is a member function with implicitthis
argument, so semantic doesn't match.You can do something like this:
to keep pointer to template function as a member of template class
该行:
有两个问题:一个是
&A::getA1
的类型为T (A::*)()const
但 getFirst 的类型输入T (*)()
。它们不兼容,因为前者是指向成员函数的指针,而后者则不是。该行的第二个问题是创建的对象仅在返回类型上有所不同。就像您无法手动声明 double (A::*getFirst)()const 和 char (A::*getFirst)()const 一样,您也无法创建会自动声明它们的模板。
该行:
double c = a.getFirst + b.getSecond;
有其自己的一组问题,这些问题可能与当前的问题相关,也可能无关。
对于这个“不回答”感到抱歉。也许如果您更多地谈论您想要实现的目标,而不是您如何实现它,我们将能够提供帮助。
The line:
Has two problems: One is that
&A<T>::getA1
is of typeT (A::*)()const
but getFirst is of typeT (*)()
. These are not compatible because the former is a pointer to a member function, while the latter is not.The second problem with the line is that the objects created would differ only in their return type. Just like you cannot manually declare both
double (A::*getFirst)()const
andchar (A::*getFirst)()const
, you also cannot create a template that would automatically declare both of them.The line:
double c = a.getFirst + b.getSecond;
Has its own set of problems that may or may not relate to the issue at hand.
Sorry for this "non answer." maybe if you talked more about what you are trying to accomplish, rather than how you are trying to accomplish it, we will be able to help.
您的代码似乎很混乱,所以我不确定我是否真的理解您所要求的内容...这是对您的编译示例的改编。
Your code seems quite confused, so I'm not sure I really understood what you are asking for... here is an adaptation of your example that compiles.