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!!!
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!!!
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.Code:class Guns { public: Guns *g; }
-Prelude
My best code is written with the delete key.
>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?
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,Code:class Guns { public: static Guns* g; // is doing this also the same? static Guns g; }
so thanks a lot for helping me out,
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:
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: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;
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...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(); }
Thats how you could use a static pointer to itself.