模板化的虚拟成员函数 [英] Templated virtual member functions
问题描述
您好,
不允许模板化虚拟成员函数。现在我正在搜索
这个问题的一个很好的解决方法,但我找不到。
这是'我的问题:
类基础
{
模板< typename T>
virtual void setParam(std :: string s,const T& value);
};
clas派生:公共基地
{
模板< typename T>
void setParam(std :: string s,const T& value);
};
Base * b = new Derived;
b-> setParam(" msg",5);
如何才能完成这项工作?
-Matthias
Hello,
templated virtual member functions are not allowed. Now I am searching
for a good workaround for this problem, but I can''t find any.
Here''s my problem:
class Base
{
template <typename T>
virtual void setParam(std::string s, const T& value);
};
clas Derived : public Base
{
template <typename T>
void setParam(std::string s, const T& value);
};
Base* b = new Derived;
b->setParam( "msg", 5 );
How can I make this work?
-Matthias
推荐答案
Matthias写道:
Matthias wrote:
模板化虚拟不允许使用成员函数。现在我正在寻找一个解决这个问题的好方法,但我找不到任何问题。
这里是我的问题:
班级基地
{
模板< typename T>
virtual void setParam(std :: string s,const T& value);
};
clas派生:public Base
{
模板< typename T>
void setParam(std :: string s,const T& value);
};
基础* b = new Derived;
b-> setParam(" msg",5);
如何使这项工作?
templated virtual member functions are not allowed. Now I am searching
for a good workaround for this problem, but I can''t find any.
Here''s my problem:
class Base
{
template <typename T>
virtual void setParam(std::string s, const T& value);
};
clas Derived : public Base
{
template <typename T>
void setParam(std::string s, const T& value);
};
Base* b = new Derived;
b->setParam( "msg", 5 );
How can I make this work?
重新设计课程。你为什么要虚拟?为什么你需要
从Base派生?你为什么需要一个模板?你在向我们展示
的解决方案。一个未知的问题。我们不知道你在尝试完成什么。如果你解释了你的设计意图,有人可能会推荐一些东西。
V
-
请在邮寄回复时从我的地址中删除资金
By redesigning your classes. Why do you need it virtual? Why do you need
to derive from Base? Why do you need it a template? You''re presenting us
with a "solution" to an unknown problem. We don''t know what you''re trying
to accomplish. If you explained your design intentions, somebody might be
able to recommend something.
V
--
Please remove capital As from my address when replying by mail
class Base
{
模板< typename T>
virtual void setParam(std :: string s,const T& value);
};
你建议v-table的大小应该是多少?
clas派生:公共基地
{
模板< typename T>
void setParam(std :: string s,const T& value);
};
Base * b = new Derived;
b-> ; setParam(" msg",5);
我怎样才能使这个工作?
class Base
{
template <typename T>
virtual void setParam(std::string s, const T& value);
};
and what size do you propose the v-table should be?
clas Derived : public Base
{
template <typename T>
void setParam(std::string s, const T& value);
};
Base* b = new Derived;
b->setParam( "msg", 5 );
How can I make this work?
你可以问ostream类怎么来流很多不同的
类型,但流是一个非虚函数。
多态性在哪里?在不同版本的
Base会发生什么?
使用ostream他们都会做一些常见的事情 - 有效地将
类型转换为一个字符串然后有不同的方式来写字符串。
You could ask how come ostream classes can stream in lots of different
types but yet streaming is a non-virtual function.
Where is the polymorphism? What will happen in different versions of
Base?
With ostream they all do something common - effectively convert the
type to a string then have different ways of writing the string.
你可以使用TypeList结构
You can use the TypeList structure
这篇关于模板化的虚拟成员函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!