Access to a private member map outside class

Oct 24, 2013 at 12:21pm
I apologize if I've chosen wrong forum to post since I'm not fully confident wheter this is beginner question or not.

This question is more from a design point-of-view rather than coding it to be a fully functional.

So here it goes:

I have multiple files which each require their own object of same class type (ref. First Class). File contents are read from a file to a unordered_map<std::string, std::vector<std::string>> which is either private or protected member inside First Class. First Class does not need any public functions to add, remove or change the data during runtime, but changes are only being made by checking if the file size has changed during the day, if the size is not equal to the last check, map gets updated.

Now, I have a Second Class which is a data handler class. It has public member functions with arguments that needs to be get from First Class's unordered_map using const_iterator. This is where I could use some guidance on which way to go with design and implementation.

I know there's two methods to do this but I'm looking for any possible tips. Re-doing handler class is also not out of the question. These two methods I'm aware of are:

1. Declare these maps to local scope, build few global functions and here we go. (Probably the easiest way.)

2. Create public member functions to a First Class which either return a pointer or a reference to a protected/private member. (I'm under the impression that I really shouldn't be doing this because of a bad coding practice.)

Note that I don't need any code here, just some other point-of-views regarding the subject itself for learning better coding practices.
Oct 24, 2013 at 12:27pm
I don't really read much of your post since I am bit sleepy right now

accessing a private member outside the class itself can only be achieved by using "friend"
but many people say friend are bad pratice

I personally thinl that it isn't
just don't overuse it

it will turn out ugly to use this "hack" everytime
since it reduces the point of using OOP system by exposing encapsulated so many times in the program
Oct 24, 2013 at 12:35pm
If you want the args, then ask for the args
1
2
3
/*const*/std::vector<std::string>& First_Class::args(std::string key){
   return map[key];
}
Last edited on Oct 24, 2013 at 12:36pm
Oct 24, 2013 at 12:47pm
How true do you want to be to the principles of C++?

1) Sure this is easy, but this defeats the principle of encapsulation. You are exposing the implementation of your unordered_map all over your program. Not a good idea.

2) Again this is exposing the implementation of your unordered_map all over your program.

The whole idea of encapsulation is that FirstClass should hide it's implementation. If you find you're having to expose it's implementation, then you have not designed the class properly (or you need another class).

FirstClass should be able to change it's storage implementation to some other container, or perhaps some persistant mechanism without impacting other code in your program.

You're using a vector of strings as the second template argument. I would suggest making that its own class. That way you again hide the implementation of the collection of strings allowing you to change the implementation of the string collection in the future without impacting anything other than that class. Lets call that a StringColl class. Your unordered map becomes simply unordered_map<string, StringColl>. Now, it should be easy to provide methods in FirstClass that access a StringColl without knowing how StringColl is implemented.




Oct 24, 2013 at 1:46pm
> It has public member functions with arguments that needs to be get from First Class's unordered_map using const_iterator.


Option 2) is one way to go.

Provide a public member function which returns a reference to const

const std::unordered_map<std::string, std::vector<std::string>>& get_it() const ;

Or a helper inspector function is another way:

1
2
const std::vector<std::string>>& look_up( const std::string& key ) const ; 
// again, note the const in the return type  
Oct 24, 2013 at 4:51pm
Thank you for all the responses.

@rmxhaha,
I don't really think that usage of "friend" would be appropriate, especially in this type of situation. Sure it would work just like the two other methods I already listed but I just don't see it to be appropriate way to go.

@AbstractionAnon,
I especially thank you for your feedback, this gave me some new ideas and thoughts to think about and maybe the handler class is here to blame for it's complexity, rather than FirstClass. I had in mind to use class as second template argument but for the sake of simplicity I used vector in example I gave in my first post.

The principles, well... I'm still trying to follow and learn why some methods are considered to be "good practices" in one point and others are "not so good" or even "bad". I do know and understand that there's plenty of opinions and sometimes you're just going to have to go with "the bad route" but as you pointed out, class design in this case is to blame.

I should also point out that I am very much beginner considering I only have about 8 months of experience with C++ and no earlier experience of programming.
Topic archived. No new replies allowed.