1

Say i have a strategy pattern with 2 classes implementing the strategy, where each has some set of values, but the values are stored in different containers, say a vector and a list.

I would like to have an iterator allowing me to go over each of the values in each class. I would also like to be able to use the standard iterator way, meaning going over the values in this manner :

Strategy s = //Some initialization.
Strategy :: iterator it;
for (it = s.begin(); it != s.end() ; ++it){
//Do something with *it.
}

for now the only solution i have is : another strategy pattern where the Strategy class has the context iterator. another strategyIterator and each "strategy implementing class" has it's own - class selfIterator : public strategyIterator.

but this seems quite a hassle, and maybe someone else has more creativity than me. So i'd really appreciate some ideas :), Thanks!

It's for a program that manipulate polynomials, where the strategy is a representation of the polynomial, one with a vector and the other strategy with a list.

Here is also the H file of the program i want to use it for :

#include <iostream>
#include <vector>
#include <list>

using namespace std;

#ifndef MYPOLY_H
#define MYPOLY_H

class PolyRep;
class RegIterator;

typedef enum Poly
{
    REG_POLY = 1, SPARSE_POLY = 2
};

/* =============================================================================
 * MyPoly class.
 * =============================================================================*/

class MyPoly
{
public:

    MyPoly(double arr[], unsigned int arrSize);

    MyPoly(double num);

    MyPoly(double X[], double Y[], int arrSize);

    MyPoly(string sPoly);

    ~MyPoly();

    string toString();

    double evaluate(double x) const;

    MyPoly derive(int n);

    MyPoly & operator =(const MyPoly &rhs);

    MyPoly & operator +=(const MyPoly &rhs);

    MyPoly & operator -=(const MyPoly &rhs);

    MyPoly & operator *=(const MyPoly &rhs);

    MyPoly operator +(const MyPoly& other);

    MyPoly operator -(const MyPoly& other);

    MyPoly operator *(const MyPoly& other);

    bool operator ==(const MyPoly& b) const;

    MyPoly operator -() const;



private:
    PolyRep * _poly;


};

/* =============================================================================
 * PolyRep class.
 * =============================================================================*/

class PolyRep
{
public:

    virtual double evaluate(const double &x) const = 0;

    //virtual iterator begin () = 0;
    //virtual PolyRep * derive(int n) = 0;



protected:
    int _degree;
};

/* =============================================================================
 * RegPoly class.
 * =============================================================================*/


class RegPoly : public PolyRep
{
public:
    RegPoly(double arr[], int degree);
    ~RegPoly();
    double evaluate(const double &x) const;

private:
    vector <double> _values;
};

/* =============================================================================
 * SparsePoly class.
 * =============================================================================*/

class SparsePoly : public PolyRep
{
public:
    SparsePoly(double arr[], int degree);
    ~SparsePoly();
    double evaluate(const double &x) const;

private:

    /*A class to represent a node in the SaprsePoly list.*/
    class SparseNode
    {
    public:
        SparseNode(int n = 0, double value = 0);
        int getN() const;
        double getValue() const;
    private:
        int _n;
        double _value;
    };
    /*End of sparse node class.*/

    list <SparseNode*> _values;
};
#endif  /* MYPOLY_H */
oopsi
  • 1,919
  • 3
  • 21
  • 28
  • 2
    `using namespace std;` is something that should be avoided at all costs in the global scope of a header file. – chris Sep 05 '12 at 00:07
  • I think the problem isn't so much `list` vs `vector` as it is `double` vs `SparseNode*`--that will require templatization that you can't resolve at runtime like you need. – Matt Phillips Sep 05 '12 at 00:58
  • 2
    Don't use variable names that start with underscores. They are reserved for compiler usage. If you want to indicate member variables, switch to _ instead. – Yuushi Sep 05 '12 at 01:22
  • @chris Thank you very much, i just figured out the problem it causes. – oopsi Sep 05 '12 at 07:30
  • @Yuushi, While good to mention, as it's easy enough to happen to use a violating name, it's not applicable here. One underscore is only reserved for the global scope. See [this question](http://stackoverflow.com/questions/228783/what-are-the-rules-about-using-an-underscore-in-a-c-identifier) for the rules and relevant standard references. – chris Sep 05 '12 at 19:01

0 Answers0