使用采用指向对象基类的函数对shared_ptr向量进行累加
我使用的是 VS2008(所以我有 TR1,但没有 C++11),并且我不使用 boost。 下面的描述是我的实际问题的简化版本。
我有以下类层次结构:一个接口、一个实现它的抽象类以及从该抽象类派生的几个类:
struct IMyInterface
{
virtual void MinMax(int& Min, int& Max)const = 0;
};
class Base : public IMyInterface // Abstract
{
// ...
};
class A: public Base
{
public:
virtual void MinMax(int& Min, int& Max)const { // Some stuff for A}
// ...
};
class B: public Base
{
public:
virtual void MinMax(int& Min, int& Max)const { // Some stuff for B}
// ...
};
然后我有一个指向基类的shared_ptr向量:
// Somewhere in my code
typedef shared_ptr<Base> Base_sp;
vector<Base_sp> v;
我有一些执行此操作的代码:
int Vmin = INT_MAX;
int Vmax = INT_MIN;
for(vector<Base_sp>::const_iterator it = v_sp.begin(); it != v_sp.end(); ++it)
{
int v1, v2;
(*it)->MinMax(v1, v2);
Vmin = std::min(v1, Vmin);
Vmax = std::max(v2, Vmax);
}
我使用类似的代码在不止一个地方,所以我将其取出到我运行的函数中:
pair<int, int> MinMaxFinder(pair<int, int> vals, Base_sp elem)
{
int Vmin = vals.first;
int Vmax = vals.second;
if (elem)
{
int v1, v2;
elem->MinMax(v1, v2);
Vmin = std::min(v1, Vmin);
Vmax = std::max(v2, Vmax);
}
return make_pair(Vmin, Vmax);
}
pair<int, int> tmp = accumulate(v_sp.begin(), v_sp.end(),
make_pair(INT_MAX, INT_MIN), &MinMaxFinder);
int Vmin = tmp.first;
int Vmax = tmp.second;
这工作正常,但是,在我使用要替换的代码的其他地方,我不一定使用 Base_sp 派生类,但其他 IMyInterface 派生类。所以我想这样声明 MinMaxFinder:
pair<int, int> MinMaxFinder(pair<int, int> vals, IMyInterface* elem)
但是,当然,它不能编译。
那么,有没有办法通过适配器或其他东西来完成我想要的事情?如果没有,关于如何解决这个问题有什么想法吗?
I'm using VS2008 (so I have TR1, but no C++11), and I don't use boost.
The description that follows is a simplified version of my real problem.
I have the following class hierarchy: an interface, an abstract class that implements it and several classes derived from that abstract class:
struct IMyInterface
{
virtual void MinMax(int& Min, int& Max)const = 0;
};
class Base : public IMyInterface // Abstract
{
// ...
};
class A: public Base
{
public:
virtual void MinMax(int& Min, int& Max)const { // Some stuff for A}
// ...
};
class B: public Base
{
public:
virtual void MinMax(int& Min, int& Max)const { // Some stuff for B}
// ...
};
Then I have a vector of shared_ptr to the Base class:
// Somewhere in my code
typedef shared_ptr<Base> Base_sp;
vector<Base_sp> v;
And I have some code that does this:
int Vmin = INT_MAX;
int Vmax = INT_MIN;
for(vector<Base_sp>::const_iterator it = v_sp.begin(); it != v_sp.end(); ++it)
{
int v1, v2;
(*it)->MinMax(v1, v2);
Vmin = std::min(v1, Vmin);
Vmax = std::max(v2, Vmax);
}
I use similar code in more than one place so I've taken it out to a function I run through accumulate:
pair<int, int> MinMaxFinder(pair<int, int> vals, Base_sp elem)
{
int Vmin = vals.first;
int Vmax = vals.second;
if (elem)
{
int v1, v2;
elem->MinMax(v1, v2);
Vmin = std::min(v1, Vmin);
Vmax = std::max(v2, Vmax);
}
return make_pair(Vmin, Vmax);
}
pair<int, int> tmp = accumulate(v_sp.begin(), v_sp.end(),
make_pair(INT_MAX, INT_MIN), &MinMaxFinder);
int Vmin = tmp.first;
int Vmax = tmp.second;
This works fine, but, in the other places where I use the code I want to replace, I do not necessarily use Base_sp-derived classes, but other IMyInterface-derived classes. So I would like to declare MinMaxFinder like this:
pair<int, int> MinMaxFinder(pair<int, int> vals, IMyInterface* elem)
But that, of course, doesn't compile.
So, is there a way, through an adapter or something, to do what I want? If not, any ideas on how to tackle this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以根据所使用的指针类型对函数进行参数化:
为了方便起见,可以将其进一步包装在由容器类型参数化的函数中:
You could parametrise the function over the type of pointer used:
For convenience, this could be further wrapped in a function parametrised by the container type: