I'm trying to plan a homework project where I have a database class and a report class, the report class will generate various reports using the data from the database class. The database class will be implemented with multiple nested classes that map key-values together using STL Maps. Now here's my question:
Is it bad programming practice to return an STL Map iterator reference(or is it pointer?) from the database class to the report class, so it can generate various reports?
The iterator created here will cease to exist as soon as the function ends, and the returned reference will be a reference to something that no longer exists. This is bad news.
Is it bad programming practice to return an STL Map iterator reference(or is it pointer?) from the database class to the report class, so it can generate various reports?
Firstly, it's a reference, not a pointer. And if you don't know the difference, then you shouldn't be using either. You should absolutely make it your first priority to learn what pointers are, and learn what references are, and learn the difference.
As Repeater has mentioned, this reference will be referring to something that has already been destroyed, so you won't be able to use it. (This would not be true if it were a const reference, because making a const reference to a temporary object extends the life of that object. But, obviously, making it const restricts what you can do with it.)
Why do you want to return a reference? What's wrong with simply returning a copy of the iterator?
The reason for returning an iterator for STL map in class A (the database class) is because class B will print to screen various reports on the data contained within multiple map containers in class A. So for class B to print these reports it needs access to the data in Class A. Something like this:
1 2 3 4 5 6 7 8 9 10 11 12 13 14
Class B {
public:
//declare an object of class A
A a;
void generateReport(){
for(std::map<std::string, int>::iterator it = a.getIterator(); it! = a.getEndIterator(); ++it) {
std::cout << it->first << " => " << it ->second << '\n';
}
}
};
So what I'm trying to figure out is this, does it go against the spirit of OOD to have code like this, where class B has access to class A(database) stl map containers through iterators?
There will be large volumes of data contained within an instance of class A, and I'm trying to figure out what would be the best approach for class B to have access to that data, which it will print to screen as a formatted report.
This is a homework project and we are restricted to having class A as a purely data-driven database and class B to generate various reports from the database.
Would this sort of implementation violate the principle of data hiding for OOD? In other words, does returning a const iterator or const reference to map violate the principle of data hiding?
Would this sort of implementation violate the principle of data hiding for OOD? In other words, does returning a const iterator or const reference to map violate the principle of data hiding?
The idea of returning an iterator was yours. The const limits the access via the iterator, i.e. is "hiding" more.
Overall, if you do hand out map<string, int>::iterator to client, the client sees that you have at least map<string, int>. A relational database could have multiple tables (maps) and a query can combine data from those tables.
In that sense the Thomas Approach is superior. The class Report has no idea how the class A stores its data. The Report simply gets pieces of data for output formatting.
The A could have "premade queries":
1 2 3 4 5
class A
{
vector<T> q1( const string& key );
// more q's
}
Each query generates a list of results somehow. The user (B) does not know how data was copied to the result. The Ts could be int, tuple, ...
Yeah I know it was mines but I honestly wasn't sure if it was a good idea since I'm still trying to grasp the concept of OOD. Thank you guys for your replies, now I have a better idea of where I stand and which direction to go.
Thanks again, this site is such a big help for us beginners!