473,409 Members | 1,979 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,409 software developers and data experts.

static keyword inside member function

Hi,

I have the following line inside one of my class member functions:

static bool tested = false;
if (!tested) {
MessageBox(NULL, "Hi", "Testing", MB_OK |
MB_ICONEXCLAMATION);
tested = true;
}

The idea, of course, is to have this message box only pop up the first
time the member function is called. However, it doesn't work: the
message box pops up everytime. I know using static inside a normal
function causes the variable to persist. Is there some reason it
won't work inside a member function? Any way I can get it to work?

Thanks for any help,
cpp
Jul 22 '05 #1
2 2234
On Sun, 02 May 2004 18:59:25 GMT, cppaddict <he***@hello.com> wrote:
Hi,

I have the following line inside one of my class member functions:

static bool tested = false;
if (!tested) {
MessageBox(NULL, "Hi", "Testing", MB_OK |
MB_ICONEXCLAMATION);
tested = true;
}

The idea, of course, is to have this message box only pop up the first
time the member function is called. However, it doesn't work: the
message box pops up everytime. I know using static inside a normal
function causes the variable to persist. Is there some reason it
won't work inside a member function? Any way I can get it to work?
Strange, this looks like it should work fine. There's nothing special about
statics in member functions vs. other functions. See if this works for you;
it should only put up the "dialog box" once:

#include <iostream>
using namespace std;

void stat_test()
{
static bool tested = false;
if (!tested) {
cout << "Displaying dialog box!" << endl;
tested = true;
}
}

int main()
{
stat_test();
stat_test();
stat_test();
return 0;
}

HTH,
-leor

Thanks for any help,
cpp


--
Leor Zolman --- BD Software --- www.bdsoft.com
On-Site Training in C/C++, Java, Perl and Unix
C++ users: download BD Software's free STL Error Message Decryptor at:
www.bdsoft.com/tools/stlfilt.html
Jul 22 '05 #2
Strange, this looks like it should work fine. There's nothing special about
statics in member functions vs. other functions. See if this works for you;
it should only put up the "dialog box" once:


Leo,

It was the message box screwing things up. If I do:

static bool tested = false;
if (!tested) {
tested = true;
MessageBox(NULL, "Hi", "Testing", MB_OK |
MB_ICONEXCLAMATION);
}

it works fine.

Thanks,
cpp

Jul 22 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

29
by: Alexander Mahr | last post by:
Dear Newsgroup, I'm somehow confused with the usage of the static keyword. I can see two function of the keyword static in conjunction with a data member of a class. 1. The data member...
7
by: lallous | last post by:
Hello, Why is the static variable inside a member function static for all instances and not static for the current instance of the object? -- Elias
2
by: katekukku | last post by:
HI, Could anyone please tell me what are static variables and what exactly are there features. I am a little bit confused. Thank You
9
by: Bryan Parkoff | last post by:
I have noticed that C programmers put static keyword beside global variable and global functions in C source codes. I believe that it is not necessary and it is not the practice in C++. Static...
5
by: Martin Vorbrodt | last post by:
so if i have this: // header.h class X { public: static int s = 1; }; i still need this:
9
by: wizwx | last post by:
There are two typical implementations of a singleton. The first one is to use a static pointer class Singleton { static Singleton * pSingleton; public: static Singleton * instance() {...
14
by: Jess | last post by:
Hello, I learned that there are five kinds of static objects, namely 1. global objects 2. object defined in namespace scope 3. object declared static instead classes 4. objects declared...
5
by: Hong Ye | last post by:
Traits is a useful template technique to simplfy the implementation of some classes. however, I met some questions when I tried to understand and implement it. Following is an example of traits...
27
by: arkmancn | last post by:
Any comments? thanks. Jim
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.