Static variable inside member function?

This is a discussion on Static variable inside member function? within the C++ Programming forums, part of the General Programming Boards category; Code: void Test::init() { static int times_called = 0; ++times_called; // This should never happen! assert(times_called > 1); } I ...

  1. #1
    Registered User
    Join Date
    Mar 2009
    Posts
    399

    Static variable inside member function?

    Code:
    void Test::init()
    {
        static int times_called = 0;
        ++times_called;
    	
        // This should never happen!
        assert(times_called > 1);
    }
    I only have one single call to the init function in the code, and I still get this:
    Code:
    Assertion failed: (times_called > 1), function init, file ...
    Program received signal:  “SIGABRT”.
    mi_cmd_stack_list_frames: Not enough frames in stack.
    mi_cmd_stack_list_frames: Not enough frames in stack.
    Can't you use static variables like this inside class member functions?

    EDIT: Even without the assert I still get the "not enough frames" error.
    Last edited by Memloop; 11-26-2009 at 09:49 AM.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,593
    I note that the correct assertion is:
    Code:
    assert(times_called > 0);
    Anyway, I am unable to duplicate your error:
    Code:
    #include <cassert>
    
    class Test
    {
    public:
        void init();
    };
    
    int main()
    {
        Test test;
        test.init();
    }
    
    void Test::init()
    {
        static int times_called = 0;
        ++times_called;
    
        // This should never happen!
        assert(times_called > 0);
    }
    Post the smallest and simplest (compilable) program that demonstrates the error.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Mar 2009
    Posts
    399
    You're right, changing the assert (or setting times_called to 1 instead) makes the code run. I guess the "out of frames" error was from the debugger for some reason.

  4. #4
    Registered User
    Join Date
    Mar 2009
    Posts
    399
    I have one question though: Does the static variable inside the function apply to all instances of the class, or will it be initialized every time a new instance is created?

  5. #5
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,538
    Code:
    #include <iostream>
    
    class foo
    {
    public:
    	void foo2()
    	{
    		static int n = 0;
    		std::cout << &n << std::endl;
    	}
    };
    
    int main()
    {
    	foo foo1;
    	foo foo2;
    	foo foo3;
    	foo1.foo2();
    	foo2.foo2();
    	foo3.foo2();
    }
    Output:
    01317138
    01317138
    01317138
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 04-22-2008, 12:07 PM
  2. Replies: 6
    Last Post: 12-13-2007, 07:20 PM
  3. <Gulp>
    By kryptkat in forum Windows Programming
    Replies: 7
    Last Post: 01-14-2006, 12:03 PM
  4. Problem with Visual C++ Object-Oriented Programming Book.
    By GameGenie in forum C++ Programming
    Replies: 9
    Last Post: 08-29-2005, 11:21 PM
  5. Interface Question
    By smog890 in forum C Programming
    Replies: 11
    Last Post: 06-03-2002, 05:06 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21