Thread: Class Scope & Locking question

  1. #1
    Registered User
    Join Date
    Aug 2008

    Question Class Scope & Locking question

    Alright I am going in circles here and cannot find a definitive answer to my question online... I have the following classes...

    Class A:
    #include classB.h;
    classA :
      classB B;
    Class B:
    #include mutexClass.h
    classB :
      mutexClass mutex;
      bool test;

    #include <pthread.h>
    mutexClass :
      mutexClass ();
      ~mutexClass ();
    //The (un)lock are basic calls to the pthread to 
    //check if its either locked or unlocked...
    MAIN :
    #include "classA.h"
    classA = 0;
    int main()
      A = new classA;
     {do something change bool value}
      if(A->B.mutex.unlock())  //Will check to see if unlocked.
     {do something and lock/unlock}
    "more stuff"
    I know that was alot of code sorry...
    First off; test and/or the mutex are being used within classB to set their value depeding on different criteria. Secondly I am trying to find which would be the best to use. Thirdly I want to do it this way so I have a better understanding of class scope, and class pointers since I am still very uncertain when one should use a class pointer or not..

    Now, on to my question. Would calling the mutex in the if statement with the function not being a pointer cause the mutex to lose its value since the destructor is called at the end of class scope that is not a pointer? Also can I access the same data via the if(A->B.test) call or is that value just going to be a default value since when being constructed the value is initialized again without any prior knowledge to its previous value.

    Would the value for test carry through or would its value be deleted and reistantiate upon the call from main even if classB set its value to something?

  2. #2
    Join Date
    Oct 2007
    Inside my computer
    Erm, lots of questions that can be answered with a simple answer. You seem to have problems understanding scope.
    A scope begins with a { and ends with a }. Any variables inside that scope will be destroyed when the scope ends. If you wish to extend the lifetime of something outside a scope, use a pointer and allocate the object on the heap. The pointer will go out of scope, but the object will not. Remember to keep track to it so you don't result in a memory leak.
    Also, don't initialize pointers with 0; use nullptr.
    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.

  3. #3
    Registered User
    Join Date
    Aug 2008
    I see... Since the test value will remain if declared as a pointer, I want to know what happens to the mutex class which is called... Will the class also be destoryed, and if so do the pthead calls act like points so when the class is instantiated again it will still have the original value of the mutex?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Whats the best way to control a global class?
    By parad0x13 in forum C++ Programming
    Replies: 3
    Last Post: 11-12-2009, 05:17 PM
  2. Question about redefining functions in derived class
    By Sharke in forum C++ Programming
    Replies: 2
    Last Post: 08-05-2009, 11:48 AM
  3. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 08:18 PM
  4. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  5. deriving classes
    By l2u in forum C++ Programming
    Replies: 12
    Last Post: 01-15-2007, 05:01 PM