473,399 Members | 2,146 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,399 software developers and data experts.

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,float>* bData;

void myFunction(){ //get data from *bData
map<string,floattemp = *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
Aug 1 '08 #1
3 1052
tr******@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,float>* bData;

void myFunction(){ //get data from *bData
map<string,floattemp = *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"];
}
};

--
Ian Collins.
Aug 1 '08 #2
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,stringbData 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.comwrote:
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,float>* bData;
void myFunction(){ //get data from *bData
* * map<string,floattemp = *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"];
* }

};

--
Ian Collins.
Aug 1 '08 #3
tr******@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.comwrote:
>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,stringbData 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.
Aug 1 '08 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
by: Dmitry D | last post by:
Hi, I'm new to C++ (started learning in the beginning of this summer), and I have the following question (sorry if it sounds stupid): In many code samples and source files, I see NULL expression...
4
by: Asif | last post by:
Hi there, I have been trying to understand the behaviour of char (*pfn)(null) for a couple of days. can some body help me understand the behaviour of char (*pfn)(null) in Visual C++ environment?...
22
by: lokman | last post by:
Hi, In the following code, can someone tell me the difference between *p++ and p++ ? I can see both achieve the same result. Thanks a lot !
20
by: __PPS__ | last post by:
Hello everybody in a quiz I had a question about dangling pointer: "What a dangling pointer is and the danger of using it" My answer was: "dangling pointer is a pointer that points to some...
8
by: Xingbo G | last post by:
The question 2.11 of C FAQ describes how we can read/write structures from/to data files. However, there is a sentence that doesn't make sense to me - <What's important is that fwrite receive a...
4
by: anonymous | last post by:
Thanks your reply. The article I read is from www.hakin9.org/en/attachments/stackoverflow_en.pdf. And you're right. I don't know it very clearly. And that's why I want to understand it; for it's...
1
by: rahul8143 | last post by:
hello, In kernel source code there is ip_fragment.c file my question is regarding pointer function and casting for that look at required snippet from that file There is structure defined for...
14
by: streamkid | last post by:
i'm a learning newbie at c++... and i have the following question... reading some source code, i saw this: int function(const void * one, const void * two) { int var1, var2; var1 =...
25
by: Why Tea | last post by:
Thanks to those who have answered my original question. I thought I understood the answer and set out to write some code to prove my understanding. The code was written without any error checking....
2
by: Giorgos Keramidas | last post by:
On Sun, 05 Oct 2008 18:22:13 +0300, Giorgos Keramidas <keramida@ceid.upatras.grwrote: My apologies. I should have been less hasty to hit `post'. If showtext() is passed a null pointer, it may...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.