在 C++您可以在子类中使用不同的参数值扩展参数化基类吗?
在我理解的所有语言中,这是不可能的,但有人告诉我在 C++ 中这是可能的,但我很难相信它。本质上,当您参数化一个类时,您是在编译阶段创建一个唯一的类,不是吗?
如果我的问题不清楚,请告诉我。
这是我尝试解释我想要做的事情(注意类 L ):
//; g++ ModifingBaseClassParameter.cpp -o ModifingBaseClassParameter;ModifingBaseClassParameter
#include <iostream>
using namespace std;
template<typename T>
class Base
{
public:
Base() {}
Base(T& t) : m_t(t) {}
T& getMem() {return m_t;}
private:
T m_t;
};
template<typename T>
class F: Base<T>
{};
template<typename T>
class L: F<long>
{};
int main()
{
Base<int> i;
F<float> f;
L<long> l;
cout<<i.getMem()<<endl;
// cout<<f.getMem()<<endl; // why doesn't this work
// cout<<l.getMem()<<endl; // why doesn't this work
}
所以正如你所看到的(希望我的语法有意义)类 L 试图将其父级的 float 参数重新定义为 long 。这看起来当然不合法,但我不同意专家的观点。
In all the languages that I understand this is not possible but someone was telling me it was possible in C++ but I have a hard time believing it. Essentially when you parameterize a class you are creating a unique class in the compilation stage aren't you?
Let me know if I am not being clear with my question.
Here is my attempt at explaning what I am trying to do ( pay attention to class L ):
//; g++ ModifingBaseClassParameter.cpp -o ModifingBaseClassParameter;ModifingBaseClassParameter
#include <iostream>
using namespace std;
template<typename T>
class Base
{
public:
Base() {}
Base(T& t) : m_t(t) {}
T& getMem() {return m_t;}
private:
T m_t;
};
template<typename T>
class F: Base<T>
{};
template<typename T>
class L: F<long>
{};
int main()
{
Base<int> i;
F<float> f;
L<long> l;
cout<<i.getMem()<<endl;
// cout<<f.getMem()<<endl; // why doesn't this work
// cout<<l.getMem()<<endl; // why doesn't this work
}
So as you can see (hopefully my syntax makes sense) class L is trying to redefine its parent's float parameter to be a long. It certainly doesn't seem like this is legal but I will differ to the experts.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
如果您想问是否可以在 c++ 中执行此操作:
那么是的,这是可能的。
它经常被使用,例如定义模板列表或从其他类型继承特征。
If you mean to ask whether you can do this in c++ :
then yes, it is possible.
It is very often used, for example to define template lists or inherit traits from another type.
您所要求的内容无法直接完成 - 但您可以通过使用默认模板参数来非常接近:
在这种特殊情况下,默认模板参数不会添加太多。即使为所有参数提供了默认值,您也必须提供模板参数列表来实例化模板。因此,在派生类中重写默认值通常仅对第二个及后续参数有用。
What you've asked for can't be done directly -- but you can come pretty close by using a default template parameter:
In this particular case, the default template parameter doesn't add much. You have to supply a template parameter list to instantiate a template, even if defaults are provided for all the parameters. As such, having a default that you override in a derived class is usually only useful for the second and subsequent parameters.
在 C++ 中继承默认是私有的,一旦公开,stephenmm 写的应该可以工作,除非你想问其他问题?
the inheritance is private by default in c++, once made public what stephenmm wrote should work unless you meant to ask something else ?
您正在考虑模板吗?
Are you taking about templates?
您应该尝试编译它:
X 不是类模板,因此尝试在
X 中实例化它没有任何意义,因为 X 没有要覆盖的模板参数。请记住,这
也不是类模板;它本身就是一个类,但是从模板完全实例化。您可以这样做:
但是这里不会对覆盖任何模板参数产生混淆。
也可以,但是这里 X 中的模板参数未被使用,并且没有任何内容被覆盖。
华泰
You should try compiling it:
X is not a class template, so it makes no sense to try to instantiate it in
X has no template parameters to override. Remember that
is not a class template either; it is a class in its own right, but fully instantiated from a template. You could do this:
But here there is no confusion about overriding any template parameters.
works too, but here the template parameter in X is unused, and nothing is overridden.
HTH