0

I have class following code

class Info {
    public:
        inline void append(int i) { var1.push_back(i); }
    private:
        std::list<int> var1; 
};

class Key {
    public:
        int getId() {return id};
    private:
        int id;
};

class Base {
    public:
        void& getMap() { return myMap;} 
    protected:
        map<Key*,Info*> myMap;
};


class B {
    public:
        void check(bool val,map<Key*,Info*>* = NULL) {
            // while processing I get key* as key1
            Key* key1;
            Info* info = new Info;
            info->append(1000);
            myMap.insert(std::pair<Key*,Info*>(key1,info));
        }
};

class Derived : public Base {
    public:
        void func() {
           // since Derived is subclass of Class Base so we access the myMap
           bobject.check(true,&myMap);
        }
    private:
        B bobject;
};

class Client {
  private:
      Base b_report;
  public:
     void client_func() {
         map<Key*,Info*> myMapClient = b_report->getMap();
         // will be using myMapClient;
     } 
};

Three questions

  1. is there any problem in this code. can we pass pointer of one of member variable to function of other class object
  2. How to clear the myMap of class Base
  3. when to clear the myMap of class Base
BoBTFish
  • 19,167
  • 3
  • 49
  • 76
TechEnthusiast
  • 273
  • 4
  • 18
  • 1
    Please format your post appropriately before hitting Submit. – Lightness Races in Orbit Feb 29 '16 at 12:09
  • 3
    One question per question please. – Lightness Races in Orbit Feb 29 '16 at 12:09
  • Why do you store pointers in the map? If the objects arent supposed to be owned by something else, just store the objects in the map, then you dont have to worry about clean up. – 463035818_is_not_an_ai Feb 29 '16 at 12:25
  • btw you have to be a bit careful. The type of the pointer you are passing is `map` (which happens to be the member of some other object), while in C++ you can also have a pointer to a class member as a type (see e.g. [here](http://stackoverflow.com/questions/670734/c-pointer-to-class-data-member)) which is not what you are using here. – 463035818_is_not_an_ai Feb 29 '16 at 12:30
  • [FYI] If you want the map sorted by the actual key value then you need a custom comparator otherwise the map just sorts by pointer values. I am not sure if you are doing that or not as that code is not posted but I figured I would let you know. – NathanOliver Feb 29 '16 at 13:01

1 Answers1

0
 map<Key*,Info*> myMapClient = b_report->getMap();

You are making a copy of the map.

It should be :

     map<Key*,Info*>& myMapClient = b_report->getMap();

or

     auto& myMapClient = b_report->getMap();

or

     //decltype(auto) will infer the & as well 
     decltype(auto) myMapClient = b_report->getMap();

Also:

    void check(bool val,map<Key*,Info*>* = NULL) {
        // while processing I get key* as key1
        Key* key1;
        Info* info = new Info;
        info->append(1000);

        // key1 should be set to a valid value... 
        // right now it has a garbage value, could be anything.
        // perhaps your map should be myMap<int, Info*> instead
        myMap.insert(std::pair<Key*,Info*>(key1,info));
    }

You should add a name to the second parameter myMap

void check(bool val,map<Key*,Info*>*myMap = NULL) // perhaps change NULL to nullptr (C++11)
{
   myMap->insert(std::pair<Key*,Info*>(key1,info));

}

or

void check(bool val,map<Key*,Info*>& myMap)
{
   myMap.insert(std::pair<Key*,Info*>(key1,info));
}

Perhaps consider using std::make_pair instead?

myMap.insert(std::make_pair(key1, info)); // Simpler

How to clear the myMap of class Base

Add a function in your Base class to clear the map: it should loop trough all the elements and delete the allocated Info objects (and keys if you allocated them as well), then call mymap.clear()`.

when to clear the myMap of class Base

When you are done using it.

can we pass pointer of one of member variable to function of other class object

Yes, as long as you can guaranty that the pointer is valid while the function from the other class is using it (in your case it is).

Jts
  • 3,447
  • 1
  • 11
  • 14
  • If the pointers in the map were dynamically allocated you definitely do not want to call `clear()`. You would leak all of the memory. – NathanOliver Feb 29 '16 at 13:01
  • map::emplace() is better than calling insert() with make_pair(): [http://stackoverflow.com/a/17174245/3857](http://stackoverflow.com/a/17174245/3857) – Andy Feb 29 '16 at 13:08
  • Theres an issue with map::emplace(). Basically, emplace needs to construct the object before checking if the key already exists in the map, If it doesn't exist, everything is good. However, if the key exists already, then it will destroy the object (so a temp object was created for nothing). So I'd say no, it's not always better.. Unless you can guaranty that the key does not exist. Then yeah it should be. But just saying emplace is better is not the right answer. Somewhere in the video youtube.com/watch?v=smqT9Io_bKo Scott Meyers explains it. – Jts Feb 29 '16 at 13:19