Question about pointer to the map

T

tradevol

Hi,

Here is what I want to do.

I have class A contains class B.

class A{

public:
B b;
map<string, float> myData;
}

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

map<string,float>* bData;

void myFunction(){ //get data from *bData
map<string,float> temp = *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
 
I

Ian Collins

Hi,

Here is what I want to do.

I have class A contains class B.

class A{

public:
B b;
map<string, float> myData;
}

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

map<string,float>* bData;

void myFunction(){ //get data from *bData
map<string,float> temp = *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,float>& bData;

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

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

tradevol

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,string> bData 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

Here is what I want to do.
I have class A contains class B.
public:
 B b;
 map<string, float> myData;
}
class B{
B(map* data){
 bData = data;
}
map<string,float>* bData;
void myFunction(){ //get data from *bData
    map<string,float> temp = *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,float>& bData;

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

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

};
 
I

Ian Collins

Thanks a lot, Ian. This is the kind of answer I am looking for.
[please don't top-post]
Use a reference instead of a pointer and use the reference
throughout.

So B changes to something like

class B
{
map<string,float>& 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,string> bData 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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,819
Latest member
masterdaster

Latest Threads

Top