Cheat Engine Forum Index Cheat Engine
The Official Site of Cheat Engine
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 


[C++]Explain this to me

 
Post new topic   Reply to topic    Cheat Engine Forum Index -> General programming
View previous topic :: View next topic  
Author Message
Heartless
I post too much
Reputation: 0

Joined: 03 Dec 2006
Posts: 2436

PostPosted: Sun Jan 04, 2009 11:42 am    Post subject: [C++]Explain this to me Reply with quote

What is a singleton pattern? I read it in one of my books, but they did a bad job explaining it, and so did wikipedia.

Code:
class Singleton1
{
     public:
          Singleton1& Instance()
          {
               static Singleton Obj;
               return Obj;
          }
     private
          Singleton1();
}


What is Singleton1& Insatance(), Line 4.
I know it is declaring Instance as a function, what what is the Singleton1& part?

What is static Singleton Obj, Line 6.
What dose static do in there?

What is Singleton1(), Line 10?
What dose this private function do?

_________________
What dosen't kill you, usually does the second time.
Back to top
View user's profile Send private message
HalfPrime
Grandmaster Cheater
Reputation: 0

Joined: 12 Mar 2008
Posts: 532
Location: Right there...On your monitor

PostPosted: Sun Jan 04, 2009 1:48 pm    Post subject: Reply with quote

Looks like it makes it so that you can only have 1 object of that class. When you try to make a second object, you get a pointer to the first instead.

If you did "Singleton a, b;", a and b would would just be pointers to the exact same object.

I've never heard of this before, though, so I may be wrong.

Static means that that variable is allocated once during the whole program so if it's called twice, it will be the same var.
msdn wrote:
When modifying a data member in a class declaration, the static keyword specifies that one copy of the member is shared by all instances of the class. When modifying a member function in a class declaration, the static keyword specifies that the function accesses only static members.


private means it can only be accessed by a member of that class. ie, you can't call a.Singleton1(); from the main loop. If it was under public:, then you could. There's also protected: which is like private:, but it cna also be accessed by members of child classes.

_________________
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    Cheat Engine Forum Index -> General programming All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Powered by phpBB © 2001, 2005 phpBB Group

CE Wiki   IRC (#CEF)   Twitter
Third party websites