can someone tell the difference between objective C’s self and C++ this pointer?
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
The main difference is that
thisis a keyword, whileselfis a variable. The result of this is that whilethisalways refers to the object that is executing a particular method, Objective-C methods are free to modifyselfduring execution. This is sometimes used by constructors, which setself = nilon failure.The reasons for doing so are:
self = [super init]) can see when the initialisation fails and know not to carry on in their own initialisers.Some initialisers will set
selfto a different, but valid, object. This can be used in class clusters, where the “abstract” class can generate a temporary instance while building the initialised object, but ultimately return a different object based on the properties that were built up during construction.In addition, it means that you can do the usual things with variable names that confuse everyone that you can’t do with keywords, such as defining a more local variable with the same name
selfin a code block.