Thread: hi, what does this thing means when declared inside your class?

  1. #1
    Registered User
    Join Date
    Apr 2002
    Posts
    142

    hi, what does this thing means when declared inside your class?

    class Guns
    {
    public:
    ...
    Guns g;
    ...
    }

    so um, what's the use and the meaning of that? and why would you do something like that? isn't it supposed to be this way..,

    class Guns
    {
    public:
    ...
    }g;

    hehe, thanks a lot!!!

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    Code:
    class Guns 
    { 
    public: 
      Guns g; 
    }
    Well, this would be an error. You can't declare an object of a class that is being defined, it makes no sense. You can declare a pointer to an object of the class being defined though since it doesn't point to anything until the program points it to something useful, at which point the class will have been defined.
    Code:
    class Guns 
    { 
    public: 
      Guns *g; 
    }
    The usage of this is so that you can use a Guns object inside of another Guns object. This kind of nesting is sometimes useful for objects of the same class, but not as often as objects of differing classes.

    -Prelude
    My best code is written with the delete key.

  3. #3
    Registered User
    Join Date
    Apr 2002
    Posts
    142
    >Well, this would be an error

    you mean compile error? coz am no getting any compile error,

    well maybe that kind of declaration would be not so quite useful,
    how about this?

    Code:
    class Guns 
    { 
    public: 
      static Guns* g;  // is doing this also the same? static Guns g;
    }
    anyway coz am trying to understand a manual am trying to read here, it's trying to explain the concept of how powerful inheritance and polymorphism is.., and well am having a hard time figuring out the things that are happening inside the code,

    so thanks a lot for helping me out,

  4. #4
    Registered User
    Join Date
    Feb 2002
    Posts
    114
    The usefulness u ask? I use this in a number of occasions: The singleton pattern, linked list (Next, prev pointers)

    Acctually the static Guns *gun can be quite usefull. This is when creating an singleton. Static means that this variable will be the same for all instances of the class.

    Consider this code:
    Code:
    class Guns
    {
    public:
        static Guns *Create(){
             if(m_gun == NULL) m_gun = new Guns();
             return m_gun;
        }
    
        static void Destroy(){
             if(m_gun){
                 delete m_gun;
                 m_gun = NULL;
             }
        }
    
    private:
        static Guns *m_gun;
    
        Guns();
        virtual ~Guns();
    }
    
    Guns *Guns::m_gun = NULL;
    What did this rubbish do? well I have hidded the constructor and destructor, So the only way to create and destroy this class is by using the Create and Destroy functions. Whats so special, is that whenever you want to have an instance of this class, you will always get the same instance (return by Create), whereever you are in your code.

    Code:
    // Here gun1 and gun2 will be the same object, even though they are not global.
    void Apa(){
        Guns *gun1 = Guns::Create();
    
        printf("%x\n", gun1); // supposed to write the address of gun1
    }
    
    void Bepa(){
        Guns *gun2 = Guns::Create(); 
    
        printf("%x\n", gun2); // supposed to write the address of gun2
    }
    
    void main(){
        Guns *gun3 = new Guns(); // <- should be compiler error, constructor is hidden... 
    
        Apa();
        Bepa();
    }
    A global class, without being global... Note that this code is highly teroethically and has not been tested, if you are intrested in the singleton pattern, contact me so I can supply a working template...

    Thats how you could use a static pointer to itself.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. deriving classes
    By l2u in forum C++ Programming
    Replies: 12
    Last Post: 01-15-2007, 05:01 PM
  2. <Gulp>
    By kryptkat in forum Windows Programming
    Replies: 7
    Last Post: 01-14-2006, 01:03 PM
  3. Using bitset class inside another
    By Dhekke in forum C++ Programming
    Replies: 3
    Last Post: 07-26-2005, 06:50 AM
  4. Please Help - Problem with Compilers
    By toonlover in forum C++ Programming
    Replies: 5
    Last Post: 07-23-2005, 10:03 AM
  5. Nested loop frustration
    By caroundw5h in forum C Programming
    Replies: 14
    Last Post: 03-15-2004, 09:45 PM