473,666 Members | 2,058 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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_ICONEXCLAMAT ION);
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 2245
On Sun, 02 May 2004 18:59:25 GMT, cppaddict <he***@hello.co m> 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_ICONEXCLAMA TION);
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_ICONEXCLAMAT ION);
}

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
4387
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 reffers in all objects of this class to the same data Or in other word by using the static keyword all objects of one class can share data. (This is what I want)
7
2176
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
1870
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
6358
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 keyword is useful inside struct, class, and function only unless you want to force local variable to be global variable so static is used. Do you have idea why most programmers do this? Bryan Parkoff
5
4282
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
1507
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() { if(pSingleton==NULL) pSingleton = new Singleton; return pSingleton;
14
6008
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 static inside functions (i.e. local static objects) 5. objects declared at file scope.
5
2858
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 template with specializations (copied from Nathan C. Myer's 1995 article): template <class numT> struct float_traits { }; struct float_traits<float{
27
5502
by: arkmancn | last post by:
Any comments? thanks. Jim
0
8444
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8869
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8781
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8551
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8639
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7386
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
4368
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2011
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1775
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.