Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

My class is mostly a front for a container, should I expose this fact?

In my design I have a class that is mostly a front for a std::vector<OtherClass>. Should I, in my class interface, hand over to any caller an std::iterator, hand over a reference to the whole vector or provide a set of functions to access the contents of my vector? Or approach it differently altogether?

More information: The elements in the container will be set up during initialization, and will not be added to when the program is running normally. The clients of my class will often do queries and modify the state of the OtherClass elements, though not in a time-critical fashion.

like image 473
Harald Hansen Avatar asked Sep 08 '14 18:09

Harald Hansen


1 Answers

You should create the functions to modify the vector internally. Here's why:

Depending on the Compiler, you might be running different versions of STL (Standard Template Library), which can cause unexpected issues, hypothetically they could change the way std::vector operates from when you build it in an older (or newer) version of the library and send it off.

However, I am guessing this is not a library, or a project to be potentially used with another compiler. You are safe to send a reference or pointer to the vector as long as you run and compile with the same version of STL.

like image 62
C_Plus_Plus_Programmer Avatar answered Nov 08 '22 10:11

C_Plus_Plus_Programmer