Is C & # 8594; equivalent dot notation in Objective-C

I do Box2D programming and head, I’m a complete noob for C ++ and C. I’m an Objective-C guy, and therefore it becomes very difficult for me to understand the language. Special:

-> 

Basically, I understand that this is used to refer to various methods or functions or variables / properties, as shown below:

 body->GetWorld()->DestroyBody(body); 

So, this is equivalent to dot notation in Objective-C:

 // made up example [body.world destroyBody]; 

or

 [self destroyBody:body.world]; 

Or something similar? I really don't get it. Can anyone let me know what that is. Thank you

+6
source share
5 answers

I do not know Objective-C, but I can explain the difference between -> and . in C and C ++, hope this helps.

. is an operator that allows you to access a member of an instance of struct / class. a->b matches (*a).b - so it first plays the pointer, then it accesses the instance of the instance pointed to by the pointer.

In addition, there is a case that Lucian mentioned - overloading operator->() given class. In case the class that you use overloads this operator, the behavior will be different, determined by the class - it can return almost everything that it needs.

+10
source

I don't know much about Objective-C, but I can try to give you some help about C ++: assuming you define a Foo class in C ++ using the bar() method:

 class Foo { public: void bar(); ... }; 

If you allocate an instance of Foo on the stack , you use dot notation ( . ) To call the bar() method:

 Foo f; f.bar(); 

If you have a pointer to an instance of Foo , you can use the arrow notation ( -> ) to call the bar() method:

 Foo* pf; // must point to some instance of Foo pf->bar(); 

(To complicate matters, there are also links that have syntax of values ​​and pointer semantics: if you have a reference to Foo (for example, Foo& f ), you still use dot notation: f.bar(); )

+4
source

. used to access the members of an object, -> used to access elements through a pointer. Usually . operator -> can be overloaded, that is, you can also use it on objects:

 struct X { X* other; X* operator->() {return other;} }; X x; x->other; 

In this case, x->other does not refer to x.other , but to x.other.other .: D

+2
source

No, use . to access Objective-C properties does not match with -> or . to access structure and class elements in C and C ++.

An Objective-C property accessory works with values ​​of type id (which is a pointer type), but uses special naming conventions to decide what it actually does. It can directly access the data member of the properties, making it look like -> to access the data member. Or he may look for special functions to get and / or set the value of the property, in which case he has sugar syntax for sending the message.

Except in cases of operator overloading in C ++, -> always coincides with the dereferencing of the pointer, and then refers to the mentioned member. a->b equivalent to (*a).b . b may be a data member for a member function, but the accessible member will have the exact name specified in b , and not its mutation based on any special naming convention. If b calls a member function, then it can be a virtual function that has some similarities, but does not coincide with sending messages to Objective-C. b can also be an overloaded member function in C ++ that has no equivalent in Objective-C.

Adding syntax . to access object properties in Objective-C, it violates the Objective-C project constructor that new functions should look new. Using @ , the message sending syntax [] and special keywords to define Objective-C objects are examples where Objective-C previously followed this project director.

+2
source

This is objective-c code.

 @interface Foo : NSObject { NSInteger _a; } @property (nonatomaic, assign) NSInteger a; @end @implement Foo @synthesize a = _a; @end 

You know the phrase "@synthesize". @synthesize, create the following codes.

 - (NSInteger)a { return _a; } - (void)setA:(NSInteger)aa { return _a = aa; } 

Allow access property a.

 void main() { Foo foo = [[Foo alloc] init]; foo.a = 1; } 

Must assign foo.a as 1. But the compiler is invoked as below.

 void main() { Foo foo = [[Foo alloc] init]; [foo setA:1]; } 

foo.a = 1 and [foo setA: 1] is the same. foo.a = 1 calls [foo setA: 1].

Anther written in C.

 class Foo { private: int _a; public: int getA(); void setA(const int aa); }; int Foo::getA() { return _a; } void Foo::setA(const int aa) { _a = aa; } // local allocation example. void main() { Foo foo; foo.setA(1); } // Heap allocation example. void main() { Foo *foo = new Foo(); foo->setA(1); delete foo; } // Pointer (like object objectve-c). void main() { Foo foo1; foo1.setA(1); Foo *foo2 = &foo1; foo2->setA(2); printf("result>>> %d, %d", foo1.a, foo2->a); } result>>> 2, 2 

foo1.a and foo2-> a is 2. An example of Objective-C is below.

 void main() { Foo *foo1 = [[Foo alloc] init]; foo1.a = 1; Foo *foo2 = foo1; foo2.a = 2; NSLog(@"result>>> %d, %d", foo1.a, foo2.a); } result>>> 2, 2 

Have a nice day. Thanks.

+1
source

Source: https://habr.com/ru/post/916238/


All Articles