?
Solved

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

Posted on 2003-03-05
4
Medium Priority
?
258 Views
Last Modified: 2010-04-01
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
Comment
Question by:BungoMan
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 

Expert Comment

by:SweetPoison
ID: 8077747
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
 

Author Comment

by:BungoMan
ID: 8077790
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
 
LVL 12

Accepted Solution

by:
Salte earned 200 total points
ID: 8078367
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
 

Author Comment

by:BungoMan
ID: 8093111
ah, thank you =)
0

Featured Post

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.
Suggested Courses

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question