On 14.08.2016 18:01, Joseph Hesse wrote:
> This is a simplification of my problem.
> The array Values in class Algorithm will be populated by the constructor
> reading from a data file.
> Once the array Values is populated it never changes, hence the const in
> "const int d;"
> The problem is how can I give values to Values[i].d?
> I probably have a bad design, nevertheless in the non-simplified version
> it reflects the real problem.
>
> Thank you, Joe
>
> ==========================================================
> #include <iostream>
> #include <string>
> using namespace std;
>
> class Data
> {
> private:
> const int d;
> // ...
> public:
> Data () : d(0) {} // so we don't have junk in d
> // ...
>
> friend class Algorithm;
> };
The `const` only makes `Data` non-assignable.
Have you considered that that makes it difficult to e.g. sort a sequence
of `Data` values?
> class Algorithm
> {
> private:
> Data Values[100]; // no need to use vector since 100 will never change
With the present `const`, `std::vector` enables you to put Data values
in the array.
Simply use `std::vector<>::push_back` or `std::vector<>::emplace_back`.
> // ...
> public:
> // ctor opens data file and reads ints into Values[i].d
> Algorithm(const string & DataFile); // code not shown
> // ...
> };
An instance of Algorithm is an object whose initialization reads values
from a file, and that is not copy assignable. Somehow that doesn't make
much sense to me. I think you should differentiate between algorithms
and the data they operate on, much as the standard library does.
Cheers & hth.,
- Alf