By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,194 Members | 854 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,194 IT Pros & Developers. It's quick & easy.

setting private variables

P: 3
I have a simple class where I try to modify it's variable before it's instantiated. I'm trying to keep track of the largest value that I iterate over (stored in myvar). The code below shows what I'm trying to do; is there a way to do this within the class itself and not externally through a F.set()? Thank you for your time.

Expand|Select|Wrap|Line Numbers
  1.  
  2.  
  3. #include <iostream>
  4. #include <string>
  5. #include <math.h>
  6. #include <map>
  7. #include <algorithm>
  8. using namespace std;
  9.  
  10.  
  11. class Foo{
  12.  
  13.       public:
  14.  
  15.              Foo();
  16.              ~Foo();
  17.  
  18.              map < string, float > fooMap;
  19.              map < string, float > * getFooMapPtr();
  20.              void Foo::operator()( pair< const string, float> &P );
  21.               float myvar;
  22.  
  23.  
  24. };
  25.  
  26. Foo::Foo( ){
  27.  
  28.      fooMap[ "Foo" ] = 5;
  29.      fooMap[ "Foo1" ] = 52;
  30.      fooMap[ "Foo2" ] = 53;
  31.  
  32.      myvar = 0;
  33. }
  34.  
  35. Foo::~Foo() {}
  36.  
  37. map < string, float > * Foo::getFooMapPtr(){
  38.  
  39.       return &fooMap;      
  40.  
  41. }
  42.  
  43. void Foo::operator()( pair< const string, float> &P ){
  44.  
  45.      if (myvar < P.second){
  46.  
  47.               myvar = P.second;
  48.      }
  49.  
  50. }
  51.  
  52. int main(){
  53.  
  54. Foo F;
  55.  
  56. map < string, float > * fm =  F.getFooMapPtr();
  57.  
  58. map< string, float >::iterator it;
  59.  
  60. for_each( fm->begin(), fm->end(), F);
  61.  
  62. cout << F.myvar << " is the value of myvar " << endl;
  63.  
  64. system ( "PAUSE" ); // for windows dos prompt hold
  65.  
  66. return 0;
  67. }
  68.  
  69.  
  70.  
Jun 7 '07 #1
Share this Question
Share on Google+
4 Replies


weaknessforcats
Expert Mod 5K+
P: 9,197
I'm not sure what you mean by this:
I have a simple class where I try to modify it's variable before it's instantiated.
Do mean the object itself or do you mean a member variable of the object?

Your code in main(), really doesn't need a class. Everything is public.
Jun 8 '07 #2

P: 3
I mean the member variable of the object. basically what I'm trying to do is change a private variable w/in the class member function when I iterate over it's map variable. Since the STL algorithms usually only accept binary or unary operators I can not update a class internally while they're being used.

If you run the code above you'll see that I can't change the class' private (or public for that matter) variable in this way. And I'm wondering if there's a way to do this.

EDIT - Also the example above is just that an example. I just used it to illustrate a situation where I might want to modify a class variable when a class function is being called.
Jun 8 '07 #3

weaknessforcats
Expert Mod 5K+
P: 9,197
The problem is that the algorithm creates a temporary object and uses it. The F that for_each uses is not the F created in main().

You solve this problem by using the Singleton design pattern. There is an articel about this in the Editor's Corner. Also, see the book Design Patterns by Eric Fromm, et al. Addison-Wesley 1994.

Essentially, a singleton is a class that can have one instance with a global point of access obtained by the class's Instance() method.

I added such a method to your class and went though your code looking at the places where you access a Foo ior a Foo member and changed that to access the Foo obtained by the Instance() method rather than the Foo pointed as by this.

I got your expected results.

Here is your modified code:
Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2. #include <string>
  3. #include <math.h>
  4. #include <map>
  5. #include <algorithm>
  6. using namespace std;
  7.  
  8.  
  9. class Foo{
  10.  
  11.       public:
  12.  
  13.              Foo();
  14.              ~Foo();
  15.  
  16.              map < string, float > fooMap;
  17.              map < string, float > * getFooMapPtr();
  18.              void Foo::operator()( pair< const string, float> &P );
  19.              static Foo* Instance();
  20.               float myvar;
  21.  
  22.  
  23. };
  24. Foo* instance = 0;
  25. Foo* Foo::Instance()
  26. {
  27.    if (!instance)
  28.    {
  29.       instance = new Foo;
  30.    }
  31.    return instance;
  32. }
  33. Foo::Foo( ){
  34.  
  35.      fooMap[ "Foo" ] = 5;
  36.      fooMap[ "Foo1" ] = 52;
  37.      fooMap[ "Foo2" ] = 53;
  38.  
  39.      myvar = 0;
  40. }
  41.  
  42. Foo::~Foo() {}
  43.  
  44. map < string, float > * Foo::getFooMapPtr(){
  45.  
  46.       return &fooMap;      
  47.  
  48. }
  49.  
  50. void Foo::operator()( pair< const string, float> &P ){
  51.  
  52.      if (myvar < P.second){
  53.               this->Instance()->myvar =P.second;
  54.  
  55.      }
  56.  
  57. }
  58.  
  59. void fx( pair< const string, float> &P)
  60. {
  61.  
  62. }
  63.  
  64. int main(){
  65.  
  66.  
  67.  
  68.     map < string, float > * fm =  Foo::Instance()->getFooMapPtr();
  69.  
  70. map< string, float >::iterator it;
  71.  
  72. for_each( fm->begin(), fm->end(), *Foo::Instance());
  73.  
  74. cout << Foo::Instance()->myvar << " is the value of myvar " << endl;
  75.  
  76. return 0;
  77. }
  78.  
Jun 8 '07 #4

P: 3
Thanks a lot for your time. I've heard of singletons before but never knew what they were used for :)
Jun 15 '07 #5

Post your reply

Sign in to post your reply or Sign up for a free account.