Global Access to Global Class without so many pointers

This is a discussion on Global Access to Global Class without so many pointers within the C++ Programming forums, part of the General Programming Boards category; I was wondering if there is a way to create a class as such: toBeGlobal.h Code: #ifndef _TOBEGLOBAL_ #define _TOBEGLOBAL_ ...

  1. #1
    Registered User
    Join Date
    Jul 2008
    Posts
    58

    Global Access to Global Class without so many pointers

    I was wondering if there is a way to create a class as such:

    toBeGlobal.h
    Code:
    #ifndef _TOBEGLOBAL_
    #define _TOBEGLOBAL_
    
    class toBeGlobal
    {
    public:
         toBeGlobal(){
              // Do Something
         };
         ~toBeGlobal(){};
    };
    
    #endif
    and initialize a toBeGlobal class in your main program as such:

    main.cpp
    Code:
    #include <Whatevers.h>
    #include "AccessingClass.h"
    #include "toBeGlobal.h"
    
    bool main()
    {
         toBeGlobal *test = new toBeGlobal();
         exit(0);
    }
    where AccessingClass wants to read the initialized *test toBeGlobal class

    AccessingClass.h
    Code:
    #ifndef _ACCESSINGCLASS_
    #define _ACCESSINGCLASS_
    
    class AccessingClass
    {
    public:
         AccessingClass(){};
         ~AccessingClass(){};
    
         void AccessFunction();
    };
    
    #endif
    where AccessFunction is defined:

    AccessingClass.cpp
    Code:
    #include "AccessingClass.h"
    
    void AccessingClass::AccessFunction()
    {
         // Malliate whatever's in *test toBeAccessed class
    }
    I know that I can pass a pointer to the *test class in the call of AccessingClass, but I don't want to do that. I find it very sloppy and I was hoping that there is a better way of doing it with headers

    - Please and thank you!

  2. #2
    The larch
    Join Date
    May 2006
    Posts
    3,573
    You are asking how you declare a global?

    E.g in header (or any file wanting to use the global):

    Code:
    extern toBeGlobal x;
    In one source file:
    Code:
    toBeGlobal x;
    Except, unless you have a really good reason for this, that is the sloppy way (and if you have more globals making use of other globals - good luck!)
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Can Nested class access parent class's private member?
    By meili100 in forum C++ Programming
    Replies: 4
    Last Post: 06-05-2009, 08:42 AM
  2. cannot access private member declared in class
    By newme in forum C++ Programming
    Replies: 7
    Last Post: 11-16-2008, 02:57 PM
  3. Message class ** Need help befor 12am tonight**
    By TransformedBG in forum C++ Programming
    Replies: 1
    Last Post: 11-29-2006, 10:03 PM
  4. Cannot access pointers from an array
    By Datamike in forum C++ Programming
    Replies: 4
    Last Post: 11-05-2005, 01:16 AM
  5. Replies: 4
    Last Post: 09-12-2001, 02:05 PM

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