I’ve seen this question: Class design with vector as a private/public member?, but I don’t feel that it answers the question.
I have a class called PrimeSieve that can be initialized as PrimeSieve s(10000) to perform actions such as checking whether a number under 10000 is prime or listing all the primes under 10000. In particular, I’m wondering how to perform the latter function.
Currently, I’m doing this, which I think violates the principles of OOP:
class PrimeSieve {
public:
...
std::vector<long long> primes;
The client will never need to change the vector, but how can I still allow the client to iterate through a vector of all of the primes under some number (using things like vector.size()? I’ve thought about an accessor method that returns the vector by value, but that seems inefficient.
I would define the class as:
I think the user of the class should have read-only access to the vector, so I used
const_iteratorin the typedef definition. Also note that theoperator[]is aconstmember function which means you can only access the value, but cannot modify it.Usage: