1

I have the following basic class structure:

class Distance : public Base {

public:
  using Base::Base;

  void run(int u, int v); // indices for nodes in graph
  void runAll();
};

and

class Base {
protected:
  const Graph& G;
  Matrix& results;

public:
  explicit Base(const Graph& G, Matrix& results);

  virtual ~Base() = default;

  virtual void run(int u, int v) = 0;
  virtual void runAll() = 0;

  double getResult(int u, int v) const;

  const Matrix& getResults() const;
};

where Distance basically calculates either for a node-pair (u, v) or for all possible node-pairs a distance-score which gets stored in the results Matrix.

The problem right now is the separation of the algorithm and the data structure. If I want to reuse the run-method of Distance in a second class AnotherDistance derived from Base I have to allocated the matrix 2 times (1 in Distance and 1 in AnotherDistance) which is not feasible in my case as the matrix could amount to multiple GB.

What would be the best approach to solve this? I could take the results-Matrix as an argument in the constructor (probably bad design) or maybe use move-semantics in another separate getter which would empty the matrix in the class.

xZA
  • 153
  • 1
  • 7
  • Do you need Distance and AnotherDistance to share the same space for result Matrix? If so, I'll make Distance a stand-alone class that manages Matrix and make run() and runAll() to accept objects derived from Base. – user3528438 Jan 23 '15 at 14:24

0 Answers0