I have a database access class that has 20 or so member functions. Each member function has a few return possiblilities. For every function, regardless of its return value, I need to run a line of code that resets the database. I would like to find a nice way of doing this without writing the code before every return statement. So what I have now is, schematically,
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
dbAccessClass::accessfuncion() {
int a, b, c;
database->GetInfo(&a,&b,&c); //get info from the database
if(a > b) {
database->Reset();
return -1; }
elseif (b > c) {
database->Reset();
return 0; }
else {
database->Reset();
return 1; }
Somehow I would like to get rid of all the reset statements and have it taken care of automatically whenever a member function exits. Any ideas? Thanks.
You could make a variable, say, temp to hold the value you would like to return. Like this.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
dbAccessClass::accessfunction()
{
int a, b, c, temp;
database->GetInfo(&a,&b,&c); //get info from the database
if(a > b)
temp = -1;
elseif (b > c)
temp = 0;
else
temp = 1;
database->Reset();
return temp;
}
Edit: Or you could not pass by reference. Or must you? (I'm still new and don't know absolutely all the reasons one would need to pass by reference so don't mind me if that's no reasonable)