Skip to main content
gamedev.net
🔒 Locked

Read only vector inside class

Started by Dwiel Nov 20, 2003 at 5:03 PM 5 replies 400+ views
Original Post
Dwiel
Dwiel
Hello, I am wondering if there was a way to make a vector in a class readonly to everyother class and read/write as normal for itself. I tried simply making a public, const pointer to the vector, which kinda worked, but I was unable to loop through the vector with iterators without typecasting away the const iterator by doing something like:
for(vector::iterator itor = (int *)cls.vec->begin(); itor != cls.vec->end(); ++itor){}  
if I remove the (int *) typecast, the compiler complains. If I typecast I defeat the purpose of the const. Is there a good way to do this without wrapping the vector functionallity with public member functions in my class? Thanks! Dwiel [edited by - Tazzel3d on November 20, 2003 7:18:44 PM]
Shannon Barber
Shannon Barber
use vector::const_iterator
The trade-off between price and quality does not exist in Japan. Rather, the idea that high quality brings on cost reduction is widely accepted.-- Tajima & Matsubara
Dwiel
Dwiel
Thanks...
sorry about that... I kinda feel like a fool now...

still, is there a way to prevent someone from simply typecasting a const away or is the const more just like a safeguard from people accidently altering it. If you really want to alter, you can go right ahead... with the pointer deal anyway.

I guess this makes a lot of sence

Thanks again... feel like a noob for not knowing about that... I am obviously kinda new to STL...

Dwiel
dmikesell
dmikesell
Why do other classes need to know that this class is implemented with a vector? Are you breaking encapsulation by letting everyone have access (even if it''s read-only) to the vector? Can you post your class definition (header file)?

--
Dave Mikesell Software & Consulting
fizban75
fizban75
Here's what I would do:


class MyClass
{
public:
// constructors, etc.

...

// users of the class can now do any const operation

// on the vector, but not be able to write to it.

const std::vector<int> getVector() const { return _vec; }

private:
// the class itself can do anything with the vector

std::vector<int> _vec;
};


But you should also look at your design and determine whether you actually need to expose the vector at all. What types of operations are the other classes doing on the vector? Do they really need to see the vector itself to do those operations? Can the operations be done within the encapsulating class itself and just the result returned? If you can show us your class and what you're trying to do, like dmikesell asked as well, we can probably help design a good solution.

[edited by - fizban75 on November 21, 2003 4:11:36 PM]
dmikesell
dmikesell
Actually, I was hoping you would post your real class, not a hypothetical one. I''m looking at this from more of a design perspective than an implementation perspective. It may not be necessary to expose the fact that your class is implemented in terms of a vector.

--
Dave Mikesell Software & Consulting

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.