Hi guys,
I know if you want to declare a variable locally and only have it declared once, you can use the static keyword on it, of course then you're responsible for managing it's value over time.
Is there a keyword or setup similar to 'static', for calling a function?
Typically, I am used to doing something like the following...
if(boolVariable)
{
callFunction();
boolVariable = false;
}
4 replies to this topic
#1
Posted 12 September 2010 - 01:16 PM
|
|
|
#2
Posted 12 September 2010 - 01:44 PM
If you use C++, you can declare a global variable (or local with static keyword) of some class. The initialization code of the C++ runtime will call the constructor of that class before your main function. This can simulate what you want.
I don't know any other method to achieve this without extra control variables.
I don't know any other method to achieve this without extra control variables.
#3
Posted 13 September 2010 - 06:19 AM
I think it's not possible but you can declare a useful macros to make some job for you:
you can also make a template class which will do the same.
#include <windows.h>// Sleep
#include <stdio.h> // printf
#include <conio.h> // getch
// gets name of a flag
#define FLAGNAME(a) __flag_##a
// gets name of a function
#define FUNCNAME(a) __func_##a
// declares a function - you can add some parameters there
#define FUNCTION(f) bool FLAGNAME(f) = false; void FUNCNAME(f)(/*parameters*/)
// sets a flag to call the function later
#define CALLLATER(f) do {FLAGNAME(f)=true;} while(0)
// clears a flag in case you've changed your mind to call a func
#define CLEARCALL(f) do {FLAGNAME(f)=false;} while(0)
// calls a function if condition is true
#define CONDCALL(f) do {if(FLAGNAME(f)){FUNCNAME(f)();FLAGNAME(f)=false;}} while(0)
// if statement with flag as a condition
#define IFFLAG(f) if (FLAGNAME(f))
bool terminated=false;
FUNCTION(unexpected)
{
printf("unexpected letter!\n");
}
FUNCTION(terminate)
{
terminated=true;
}
int main()
{
printf("press a to exit\n");
while (!terminated)
{
printf("press a key...\n");
while (!kbhit()) Sleep(1);
if ((getch()|32)=='a')
CALLLATER(terminate); else
CALLLATER(unexpected);
CONDCALL(unexpected);
IFFLAG(terminate)
{
printf("Are you sure (Y/N)?\n");
if ((getch()|32)=='n')
CLEARCALL(terminate);
}
CONDCALL(terminate);
}
printf("-- Process terminated --\n");
return 0;
}
you can also make a template class which will do the same.
#4
Posted 13 September 2010 - 07:27 AM
Macro horror. :|
I would use static local class, like dbug suggested and maybe wrap it in macro.
Like so.
I would use static local class, like dbug suggested and maybe wrap it in macro.
#define RUN_ONCE_BEGIN \
struct run_once { \
run_once() {
#define RUN_ONCE_END \
}}; \
static run_once __o;
void foo( int bar )
{
RUN_ONCE_BEGIN
std::cout << "foo::run_once" << std::endl;
RUN_ONCE_END
std::cout << "foo, bar=" << bar << std::endl;
}
int main( int argc, char* argv[] )
{
for ( int i = 0; i < 10; ++i )
foo( i );
return 0;
}
"output" said:
foo::run_once
foo, bar=0
foo, bar=1
foo, bar=2
foo, bar=3
foo, bar=4
foo, bar=5
foo, bar=6
foo, bar=7
foo, bar=8
foo, bar=9
foo, bar=0
foo, bar=1
foo, bar=2
foo, bar=3
foo, bar=4
foo, bar=5
foo, bar=6
foo, bar=7
foo, bar=8
foo, bar=9
Like so.
std::string s("oberq zhpu?");std::for_each(s.begin(),s.end(),[&](char&c){c=~c;c=~c-0x01/(~(c|0x20)/0x0D*0x02-0x0B)*0x0D;});std::cout<<s;
#5
Posted 15 September 2010 - 10:53 AM
I might be tempted to use functor objects and global variables.
1 user(s) are reading this topic
0 members, 1 guests, 0 anonymous users


Sign In
Create Account


Back to top









