使用采用指向对象基类的函数对shared_ptr向量进行累加

发布于 2024-12-22 07:27:27 字数 1848 浏览 2 评论 0原文

我使用的是 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

黑白记忆 2024-12-29 07:27:27

您可以根据所使用的指针类型对函数进行参数化:

template <typename MyInterfacePointer>
pair<int, int> MinMaxFinder(pair<int, int> vals, MyInterfacePointer elem)
{
    // Function body is identical to yours
    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);
}

vector<Base_sp> v_sp;   
accumulate(v_sp.begin(), v_sp.end(), 
           make_pair(INT_MAX, INT_MIN), &MinMaxFinder<Base_sp>);

vector<IMyInterface*> v_i;
accumulate(v_i.begin(), v_i.end(), 
           make_pair(INT_MAX, INT_MIN), &MinMaxFinder<IMyInterface*>);

为了方便起见,可以将其进一步包装在由容器类型参数化的函数中:

template <typename MyInterfacePointerContainer>
pair<int, int> FindMinMax(MyInterfacePointerContainer const & c)
{
    return accumulate(c.begin(), c.end(), make_pair(INT_MAX, INT_MIN),
        &MinMaxFinder<typename MyInterfacePointerContainer::value_type>);
}

vector<Base_sp> v_sp;
FindMinMax(v_sp);

vector<IMyInterface*> v_i;
FindMinMax(v_i);

You could parametrise the function over the type of pointer used:

template <typename MyInterfacePointer>
pair<int, int> MinMaxFinder(pair<int, int> vals, MyInterfacePointer elem)
{
    // Function body is identical to yours
    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);
}

vector<Base_sp> v_sp;   
accumulate(v_sp.begin(), v_sp.end(), 
           make_pair(INT_MAX, INT_MIN), &MinMaxFinder<Base_sp>);

vector<IMyInterface*> v_i;
accumulate(v_i.begin(), v_i.end(), 
           make_pair(INT_MAX, INT_MIN), &MinMaxFinder<IMyInterface*>);

For convenience, this could be further wrapped in a function parametrised by the container type:

template <typename MyInterfacePointerContainer>
pair<int, int> FindMinMax(MyInterfacePointerContainer const & c)
{
    return accumulate(c.begin(), c.end(), make_pair(INT_MAX, INT_MIN),
        &MinMaxFinder<typename MyInterfacePointerContainer::value_type>);
}

vector<Base_sp> v_sp;
FindMinMax(v_sp);

vector<IMyInterface*> v_i;
FindMinMax(v_i);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文