Title: C++ overloading operator of pointer to object
Tags: c++;pointers;operator-overloading
Question: I have written a class to contain a map, the class actually has a lot more methods but the relevant parts are basically this:
class myContainer{
st75b5:f2c1:f5d7:b954:8dc3:cee9:baf:a8c0map<int, st75b5:f2c1:f5d7:b954:8dc3:cee9:baf:a8c0string&gt; names;

st75b5:f2c1:f5d7:b954:8dc3:cee9:baf:a8c0string operator[](int num){
return names[num];
Simple [] operator overloading, and it works properly.
Then, I usually work with pointers to myContainer like myContainer* cont = new myContainer;, so I have to do something like (*cont)[num] to use the overloaded operator. Is there a way to make it cleaner? Ideally just cont[num]... but it's problematic because I know you can't overload operators on pointers. So is there a cleaner way of doing this? The best idea I had was just define a new method
st75b5:f2c1:f5d7:b954:8dc3:cee9:baf:a8c0string getName(int num){
return names[num];
Then I could do cont-&gt;getName(num), but that's even less comfortable than my other option. Is there a way to anyway overload [] on the pointer in this case?
Comment: "because `myContainer` is allocated separately" is insufficient to justify using a pointer.   Pointers are badly misused in C++, particularly by developers who are used to other languages that require initialising objects using `new`.

Comment: Containers shouldn't need to be allocated dynamically.  Generally it i considered a design flaw to `new` up a container.

Comment: @Jarod42 using an "external" function like `getName` is a good idea, I guess it will work.
I am using pointers because `myContainer` is allocated separately, and the scope I am in knows about the pointer. In the question I tried to simplify my situation.

Comment: @NathanOliver interesting, why is that bad? specifically the class I use is changed by multiple sources so I figured I should create is in one central place and let the rest of the code interact with it through a pointer, that way changes can be recognized by everyone. maybe I just don't understand what's the meaning of "container" and the naming causes confusion?

Comment: @SamVarshavchik that makes sense! thank you for the explanation

Comment: It is the purpose of containers to manage dynamically allocated objects. Managing dynamically allocated containers is like me hiring a tow-truck in order to tow my own car to work every day, with myself sitting in the driver seat and doing nothing.

Comment: `getName(cont, num);`?

Comment: But why using pointer?



{'source': 'StackExchangeDataset', 'subset': 'stackoverflow.com', 'metadata': {'AnswerCount': 0, 'CommentCount': 8, 'ContentLicense': 'CC BY-SA 4.0', 'CreationDate': '2021:00:01/29/21', 'FavoriteCount': None, 'PostTypeId': 1, 'Tags': '<c++><pointers><operator-overloading>', 'Title': 'C++ overloading operator of pointer to object', 'ViewCount': 46, 'id': 65962593, 'user_AboutMe': None, 'user_AccountId': None, 'user_DisplayName': None, 'user_Reputation': None, 'user_Views': None, 'user_id': None}}