static keyword inside member function

Discussion in 'C++' started by cppaddict, May 2, 2004.

  1. cppaddict

    cppaddict Guest

    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
    cppaddict, May 2, 2004
    #1
    1. Advertising

  2. cppaddict

    Leor Zolman Guest

    On Sun, 02 May 2004 18:59:25 GMT, cppaddict <> 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
    Leor Zolman, May 2, 2004
    #2
    1. Advertising

  3. cppaddict

    cppaddict Guest


    >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
    cppaddict, May 2, 2004
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Matt
    Replies:
    3
    Views:
    1,111
    aatishp
    Mar 15, 2012
  2. 0to60
    Replies:
    4
    Views:
    514
    jeffc
    Nov 21, 2003
  3. aling
    Replies:
    6
    Views:
    479
    Xiaobin.Huang
    Oct 30, 2005
  4. dolphin
    Replies:
    3
    Views:
    1,342
    Pete Becker
    Dec 5, 2007
  5. paul
    Replies:
    8
    Views:
    706
    Alf P. Steinbach
    Apr 30, 2009
Loading...

Share This Page