Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Problem with const

37 views
Skip to first unread message

Joseph Hesse

unread,
Aug 14, 2016, 12:01:52 PM8/14/16
to
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;
};

class Algorithm
{
private:
Data Values[100]; // no need to use vector since 100 will never change
// ...
public:
// ctor opens data file and reads ints into Values[i].d
Algorithm(const string & DataFile); // code not shown
// ...
};
==========================================================

Daniel

unread,
Aug 14, 2016, 1:11:15 PM8/14/16
to
On Sunday, August 14, 2016 at 12:01:52 PM UTC-4, 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?

How about

class Data
{
private:
const int d;
// ...
public:
Data(int val) : d(val) {}

friend class Algorithm;
};

class Algorithm
{
private:
std::vector<Data> Values;
// ...
public:
// ctor opens data file and reads ints into Values[i].d
Algorithm(const string & DataFile)
{
Values.reserve(100);

// For each integer i in Datafile
{
Values.push_back(Data(i));
}
assert(Values.size() == 100);
}
// ...
};

Although I don't think most people would worry about Data having a const
value, I think most people would be fine with d private and no public
modifier.

Daniel

Marcel Mueller

unread,
Aug 14, 2016, 3:39:43 PM8/14/16
to
On 14.08.16 18.01, Joseph Hesse wrote:
> 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.

Maybe your const is at the wrong place.

> class Algorithm
> {
> private:
> const Data Values[100]; // no need to use vector since 100 will never change
^^^^^
> // ...
> public:
> // ctor opens data file and reads ints into Values[i].d
> Algorithm(const string & DataFile); // code not shown
> // ...
> };
> ==========================================================

Of course, you still have to take some care about the initialization of
const arrays. std::array might fit your needs.


Marcel

Alf P. Steinbach

unread,
Aug 14, 2016, 6:07:27 PM8/14/16
to
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

0 new messages