Declaring classes in global space

This is a discussion on Declaring classes in global space within the C++ Programming forums, part of the General Programming Boards category; I have made a class but the problem is I want to create the class in global space (not inside ...

  1. #1
    Registered User
    Join Date
    Aug 2004
    Posts
    731

    Declaring classes in global space

    I have made a class but the problem is I want to create the class in global space (not inside main) but I can't do it like:

    ClassName test(hwnd);

    Because I don't even know what hwnd is yet. So how would I declare it in global space but still be able to do the code above, if you get what I mean?

  2. #2
    Unregistered User
    Join Date
    Sep 2005
    Location
    Antarctica
    Posts
    341
    Code:
    ClassName *test = NULL;
    
    int main()
    {
        ...
        test = new ClassName(hwnd);
        ...
        delete test;
    }

  3. #3
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    Quote Originally Posted by Rune Hunter
    I have made a class but the problem is I want to create the class in global space
    Why?
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  4. #4
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    Quote Originally Posted by Dave_Sinkula
    Why?
    Echo that!

    but:
    Code:
    Classname test;
    
    int main()
    {
      //....
      test.somefunc(hwnd);
    }

  5. #5
    Registered User
    Join Date
    Aug 2004
    Posts
    731
    Why? Sorry that is my fault, I didn't mean main. I am using win32 main function so I also have the windows callback for the messages.

    Well to make it simple, to use it in differnet parts of the program.

  6. #6
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    Pass a pointer instead?
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  7. #7
    Registered User
    Join Date
    Aug 2004
    Posts
    731
    Hmm pointers didn't work, couldn't find any functions inside the class. I'm looking into anouther way of doing this though.

  8. #8
    Registered User
    Join Date
    Aug 2004
    Posts
    731
    Quote Originally Posted by Thantos
    but:
    Code:
    Classname test;
    
    int main()
    {
      //....
      test.somefunc(hwnd);
    }
    Ya that is how I am going to do it.

  9. #9
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    Quote Originally Posted by Rune Hunter
    Hmm pointers didn't work, couldn't find any functions inside the class. I'm looking into anouther way of doing this though.
    Hm.
    Code:
    #include <iostream>
    #include <windows.h>
    
    class whatever
    {
    public:
       void init(HWND *hwnd) {}
       void foo()
       {
          std::cout << this << std::endl;
       }
    };
    
    int main()
    {
        whatever thing;
        thing.init(0);
        thing.foo();
        return 0;
    }
    
    /* my output
    0012FF84
    */
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  10. #10
    Super Moderator VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,596
    Actually having classes in global space is not all that bad. I do this for my D3D Apps because it is easier to shut the system down if the class objects are global. You don't have to do it this way, but I find it easier.

    Objects like Keyboard, Mouse, Device, Sound, are all global in my app. Note that only the instance of these classes is global but the definitions are in standard C++ header files and use a lot of object oriented techniques.

    Where you instantiate your objects or how visible you want them to be is purely up to you and is more based on application structure more than a right or wrong way to do things.

    Use what works.

  11. #11
    ~Team work is the best!~ wakish's Avatar
    Join Date
    Sep 2005
    Posts
    85
    using "class in global space" is not really object-oriented!
    The true essence of C++ is object-oriented...then why not use it!
    Use oop techniques, and you will find your way of programming to be better

  12. #12
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    Having a global object don't make something not OOP.
    The idea behind object-oriented programming is that a computer program is composed of a collection of individual units, or objects, as opposed to a traditional view in which a program is a list of instructions to the computer. Each object is capable of receiving messages, processing data, and sending messages to other objects.
    It doesn't matter where the object resides as long as you are only using the interface its all good.

  13. #13
    Registered User
    Join Date
    Aug 2004
    Posts
    731
    Quote Originally Posted by Bubba
    Actually having classes in global space is not all that bad. I do this for my D3D Apps because it is easier to shut the system down if the class objects are global. You don't have to do it this way, but I find it easier.

    Objects like Keyboard, Mouse, Device, Sound, are all global in my app. Note that only the instance of these classes is global but the definitions are in standard C++ header files and use a lot of object oriented techniques.

    Where you instantiate your objects or how visible you want them to be is purely up to you and is more based on application structure more than a right or wrong way to do things.

    Use what works.

    Ya that is what I am doing, don't ask why I didn't just explain I was making a d3d classes before, I havn't been thinking clearly lately.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Out of space when compiling kernel
    By NuNn in forum Linux Programming
    Replies: 3
    Last Post: 04-01-2009, 02:43 PM
  2. Classes with Other Classes as Member Data
    By njd in forum C++ Programming
    Replies: 2
    Last Post: 09-27-2005, 09:30 AM
  3. Exporting VC++ classes for use with VB
    By Helix in forum Windows Programming
    Replies: 2
    Last Post: 12-29-2003, 04:38 PM
  4. Prime Number Generator... Help !?!!
    By Halo in forum C++ Programming
    Replies: 9
    Last Post: 10-20-2003, 07:26 PM
  5. include question
    By Wanted420 in forum C++ Programming
    Replies: 8
    Last Post: 10-17-2003, 03:49 AM

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