• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 266
  • Last Modified:

making a global variable that can only be modified by one function

i need to make a global variable that is to be used by multiple functions, but i want there to be one specific function that increments the variable when needed. and i need to make it so nothing else but that function can modify the variable... here is kinda what i had in mind (not actual code, just an example of what i mean)

#include <iostream>
#include <vector>

using namespace std;

class bungo
{
     public:
          bungo();
          int uid;
};

bungo::bungo()
{
     uid=0;
}

class yarbi
{
     public:
          yarbi();
          int uid;
};

yarbi::yarbi()
{
     uid=0;
}

int getuid()

int currentuid=0; //this is the variable i need to make modifiable by only one function

void main()
{
     int counter;
     vector <bungo> bungolist(5);
     vector <yarbi> yarbilist(5);

     for (counter=0;counter<bungolist.size();counter++)
     {
          bungolist[x].uid=getuid();
     }

     for (counter=0;counter<yarbilist.size();counter++)
     {
          yarbilist[x].uid=getuid();
     }
     
     for (counter=0;counter<bungolist.size();counter++)
     {
          cout << "Bungo number " << counter << "'s uid is " << bungolist[x].uid << "." << endl;
     }

     for (counter=0;counter<yarbilist.size();counter++)
     {
          cout << "Yarbi number " << counter << "'s uid is " << yarbilist[x].uid << "." << endl;
     }
}

//and this is the only function i want to modify that variable
int getuid()
{
     currentuid++;
     return currentuid;
}
0
BungoMan
Asked:
BungoMan
  • 2
1 Solution
 
SweetPoisonCommented:
HI BungoMan
Declare the functions as CONST , so you wont be able to modify the variable currentuid . & then use the keyword MUTABLE , you can modify the value of currentuid inside your required function . I hope you get it .....

-Hari
0
 
BungoManAuthor Commented:
hmmm i think i understand what you mean, but im not sure as to where id put mutable, i think id put the const after the prototype and dectaration of getuid(), but i might have misunderstood you, thanks for pointing me in the right direction though
0
 
SalteCommented:
Actually it's easier than that.

Just place your global variable in a class:

class CurrentUid {
private:
   int uid_;

public:
   CurrentUid() : uid_(0) {}

   int getuid() { return ++uid_; }
   int uid() const { return uid_; }
};

CurrentUid currentuid;

Now the value is globally visible by currentuid.uid() but only currentuid.getuid() will modify the value. No other function can modify it.

If you want to limit access always put the member in a class.

I can't see why mutable should be necessary here. mutable is used to say that a member can be modified even if the element is const. However, if the element is mutable anyone who has access to it can modify the value. If you have a class with only mutable members, then the class can be const or not, it doesn't make a difference so you gain nothing by declaring it const. You can still modify the value as you please because it is mutable. So mutable here is a detour I think.

Just wrap the variable inside a class and make it private so that only functions in that class can access it, then use a public access function to read the value and have a function to modify the value. If you want to, you can also limit the access to the function that modifies the value by declaring it private or protected and using friend statement to grant access to those who can use it. However, if you use friend you should be aware that they have full access to all private elements, including the variable directly so they don't have to use the function.

If you want to force them to use the function and only grant access to a limited set of functions to call that function you have to make two classes. One that contain the value which is made private and have a public modification function. Then another class that has as member an object of type of the previous class. Then this second class can have a friend declaration:

class CurUid {
private:
   CurrentUid uid_;

   friend class foo;
   friend void bar();
public:
    int uid() const { return uid_.uid(); }
};

CurUid currentuid;

Now anyone can read the value of the uid, but only member functions of the class foo and the function void bar() can call uid_.getuid() to modify the uid the value. They can only modify it using that function and cannot access the integer value itself.

Alf
0
 
BungoManAuthor Commented:
ah, thank you =)
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now