CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7

Thread: static

  1. #1
    Join Date
    Oct 2001
    Posts
    745

    static

    I have a class say ,c_test
    like this

    class Test
    {
    Test()
    {
    m_pCycleBuffer = &Buffer// a valid value;

    }

    inline CycleBuffer& GetCycleBuffer() { return *m_pCycleBuffer; }

    protected:

    static CycleBuffer* m_pCycleBuffer;

    }
    In the constructor of this class Iam setting "m_pCycleBuffer" to some value.
    Iam creating 2 instances of this class Test in 2 different places.

    With the first instance when I call "GetCycleBuffer()", it returns me with the value already set in the pointer.
    But when I call the "GetCycleBuffer()" with the 2nd instance ,it returns me 0.(It is declared as a static variable)

    Why is this so?
    Could any one help?

  2. #2
    Join Date
    Apr 1999
    Location
    Altrincham, England
    Posts
    4,470
    What are you trying to do here? Your code is not going to work the way you want, if it works at all.

    You need to make class CycleBuffer a Singleton, forget the pointer in Test, and just get the singleton instance when you need it.

    Code:
    // CycleBuffer.h
    class CycleBuffer
    {
    public:
        static CycleBuffer* Instance();
        // etc.
    };
    
    // CycleBuffer.cpp
    CycleBuffer* CycleBuffer::Instance()
    {
        static CycleBuffer instance_;
        return &instance_;
    }
    Now, when you want the CycleBuffer, just use CycleBuffer::Instance().
    Correct is better than fast. Simple is better than complex. Clear is better than cute. Safe is better than insecure.
    --
    Sutter and Alexandrescu, C++ Coding Standards

    Programs must be written for people to read, and only incidentally for machines to execute.

    --
    Harold Abelson and Gerald Jay Sussman

    The cheapest, fastest and most reliable components of a computer system are those that aren't there.
    -- Gordon Bell


  3. #3
    Join Date
    Oct 2001
    Posts
    745

    doubt

    In my class,which looks like this

    // CycleBuffer.h
    class CycleBuffer
    {
    public:

    CycleBuffer::CycleBuffer(size_type bufferSize)
    {

    // I have some variable initialisations here.

    }
    static CycleBuffer* Instance();

    };

    // CycleBuffer.cpp
    CycleBuffer* CycleBuffer::Instance()
    {
    static CycleBuffer theBuffer(BUF_SIZE);
    return &theBuffer;
    }




    so when ever I call CycleBuffer::Instance(), it should always return me with the same adress & it should be going to the constructor only once.Is that so.

    But In one place where I called the CycleBuffer::Instance,it returned me with a different adress than before & it went through the constructor as well.(another instance is getting created.)

  4. #4
    Join Date
    Apr 1999
    Location
    Altrincham, England
    Posts
    4,470

    Re: doubt

    Originally posted by Kohinoor24
    so when ever I call CycleBuffer::Instance(), it should always return me with the same adress & it should be going to the constructor only once.Is that so.

    But In one place where I called the CycleBuffer::Instance,it returned me with a different adress than before & it went through the constructor as well.(another instance is getting created.)
    It certainly should always return the same address. You should see the constructor being called the first time that Instance() is called, and never thereafter.

    I can't say what might cause the behaviour you're seeing without knowing more about the application. As a start I'd put a break on the constructor and have a look at the call stack. You could try adding a destructor as well and see if that gets called when it shouldn't.
    Correct is better than fast. Simple is better than complex. Clear is better than cute. Safe is better than insecure.
    --
    Sutter and Alexandrescu, C++ Coding Standards

    Programs must be written for people to read, and only incidentally for machines to execute.

    --
    Harold Abelson and Gerald Jay Sussman

    The cheapest, fastest and most reliable components of a computer system are those that aren't there.
    -- Gordon Bell


  5. #5
    Join Date
    Oct 2001
    Posts
    745
    It is a multithreaded program.Does it make any difference?

  6. #6
    Join Date
    Oct 2001
    Posts
    745
    It is a multithreaded program.In the main thread,Iam creating 2 threads.If I call the class instance from the main thread as well as from 1 of the created thread,it creates only 1 instance.But when I call it from the second created thread,it goes to the constructor again...

  7. #7
    Join Date
    Jun 2002
    Location
    Germany
    Posts
    1,557

    confusing interplay between static members and constructors

    Hello Kohinoor24!

    Graham: Comments right on, as usual!

    Kohinoor24: Please study the following sample program.

    I think it will help you to understand the interplay between static member variables and class constructors. Please note that your class constructor will be called for every instance of the class CycleBuffer, every time, without concern for whatever static member variables are defined within the class. The constructor is always called. A static member variable in C++ can be initialized once, for example, in an initialization statement or in a line of code which is guaranteed to run one single time and before the contents of the static variable are needed elsewhere. Each modification of the static is carried out on the single instance of the static.

    Try to initialize your static at one point in your entire program and use it consistently throughout the program. See sample program below.

    Best regards.
    Chris.

    #include <iostream>
    using namespace std;

    class A
    {
    public:

    A() { ++the_static; }

    private:

    static int the_static;

    public:

    friend void print_a(const A& a);

    };

    int A::the_static = 1234;

    void print_a(const A& a)
    {
    cout << a.the_static << endl;
    }

    int main(int argc, char* argv[])
    {
    A a1;
    print_a(a1);

    A a2;
    print_a(a2);

    return 1;
    }

    You're gonna go blind staring into that box all day.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured