Question about pointer to the map

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • tradevol@yahoo.com

    Question about pointer to the map

    Hi,

    Here is what I want to do.

    I have class A contains class B.

    class A{

    public:
    B b;
    map<string, floatmyData;
    }

    class B{
    B(map* data){
    bData = data;
    }

    map<string,floa t>* bData;

    void myFunction(){ //get data from *bData
    map<string,floa ttemp = *bData;

    }

    }

    my purpose is to have data udpated continuously on class A on map
    myData, since its pointer is passed to B. B can call myFunction and be
    sure it is using the most current data from myData after find call.

    new to C++, made a mess in the codes. It seems I cannot even assign
    temp after deference bData.

    Any suggestions?

    Thanks

    Chris
  • Ian Collins

    #2
    Re: Question about pointer to the map

    tradevol@yahoo. com wrote:
    Hi,
    >
    Here is what I want to do.
    >
    I have class A contains class B.
    >
    class A{
    >
    public:
    B b;
    map<string, floatmyData;
    }
    >
    class B{
    B(map* data){
    bData = data;
    }
    >
    map<string,floa t>* bData;
    >
    void myFunction(){ //get data from *bData
    map<string,floa ttemp = *bData;
    >
    Dodgy design issues aside, you really don't want to do this! You are
    attempting to copy the map when all you probably want to do is access
    the data.

    Use a reference instead of a pointer and use the reference throughout.

    So B changes to something like

    class B
    {
    map<string,floa t>& bData;

    B(map& data)
    : bData( data ) {}

    void myFunction()
    {
    // access map.
    //
    float test = bData["test"];
    }
    };

    --
    Ian Collins.

    Comment

    • tradevol@yahoo.com

      #3
      Re: Question about pointer to the map

      Thanks a lot, Ian. This is the kind of answer I am looking for.

      One more C++ 101 question:

      I am not quite comfortable with reference usage. So when I assign
      map<string,stri ngbData with a reference to data, even if I treat
      bData inside class B as a object instead of pointer (as calling
      bData.find() instead of bData->find()), behind the scene, is only the
      pointer passed (instead of making a local copy)?

      Thanks

      Chris

      On Aug 1, 3:33 pm, Ian Collins <ian-n...@hotmail.co mwrote:
      trade...@yahoo. com wrote:
      Hi,
      >
      Here is what I want to do.
      >
      I have class A contains class B.
      >
      class A{
      >
      public:
       B b;
       map<string, floatmyData;
      }
      >
      class B{
      B(map* data){
       bData = data;
      }
      >
      map<string,floa t>* bData;
      >
      void myFunction(){ //get data from *bData
          map<string,floa ttemp = *bData;
      >
      Dodgy design issues aside, you really don't want to do this!  You are
      attempting to copy the map when all you probably want to do is access
      the data.
      >
      Use a reference instead of a pointer and use the reference throughout.
      >
      So B changes to something like
      >
      class B
      {
        map<string,floa t>& bData;
      >
        B(map& data)
         : bData( data ) {}
      >
        void myFunction()
        {
          // access map.
          //
          float test = bData["test"];
        }
      >
      };
      >
      --
      Ian Collins.

      Comment

      • Ian Collins

        #4
        Re: Question about pointer to the map

        tradevol@yahoo. com wrote:
        Thanks a lot, Ian. This is the kind of answer I am looking for.
        >
        [please don't top-post]
        On Aug 1, 3:33 pm, Ian Collins <ian-n...@hotmail.co mwrote:
        >Use a reference instead of a pointer and use the reference
        >throughout.
        >>
        >So B changes to something like
        >>
        >class B
        >{
        > map<string,floa t>& bData;
        >>
        > B(map& data)
        > : bData( data ) {}
        >>
        > void myFunction()
        > {
        > // access map.
        > //
        > float test = bData["test"];
        > }
        >>
        >};
        One more C++ 101 question:
        >
        I am not quite comfortable with reference usage. So when I assign
        map<string,stri ngbData with a reference to data, even if I treat
        bData inside class B as a object instead of pointer (as calling
        bData.find() instead of bData->find()), behind the scene, is only the
        pointer passed (instead of making a local copy)?
        >
        Yes, there isn't any copying going on.

        --
        Ian Collins.

        Comment

        Working...