[英]Is C's -> equivalent to dot notation in Objective-C
I am doing Box2D programming, and heads up, I am a total noob to C++ and C. I am an Objective-C guy, and that is why it is becoming really hard for me to understand the language. 我正在进行Box2D编程,并且抬头,我是C ++和C的总菜鸟。我是一个Objective-C家伙,这就是为什么我对语言的理解变得非常困难。 Especially:
特别:
->
Basically I understand that this is used for referencing different methods or functions or variables/properties like below: 基本上我理解这用于引用不同的方法或函数或变量/属性,如下所示:
body->GetWorld()->DestroyBody(body);
So is this equivalent to dot notation in Objective-C: 这相当于Objective-C中的点符号:
// made up example
[body.world destroyBody];
or 要么
[self destroyBody:body.world];
Or something similar? 或类似的东西? I really don't understand this.
我真的不明白这一点。 Can someone give me a heads up on what this is.
有人可以告诉我这是什么。 Thanks!
谢谢!
I don't know Objective-C, but I can explain difference between ->
and .
我不知道Objective-C,但我可以解释
->
和之间的区别.
in C and C++, hope that helps. 在C和C ++中,希望有所帮助。
.
is operator that allows you to access member of struct/class instance. 是允许您访问struct / class实例的成员的运算符。
a->b
is the same as (*a).b
- so it first dereferences the pointer, then accesses member of instance that the pointer was pointing to. a->b
与(*a).b
- 因此它首先取消引用指针,然后访问指针所指向的实例的成员。
Also, there is a case that Luchian has mentioned - overloading of operator->()
of given class. 此外,有一种情况是Luchian提到 - 重载
operator->()
给定类。 In case when class you are using does overload this operator, the behavior will be different, defined by the class - it can return virtually everything it wants. 如果你正在使用的类确实重载了这个操作符,那么行为将是不同的,由类定义 - 它几乎可以返回它想要的所有内容。
I don't know much about Objective-C, but I can try to give you some help about C++: assuming that you define a class Foo
in C++, with a method bar()
: 我对Objective-C了解不多,但我可以尝试给你一些关于C ++的帮助:假设你用C ++定义一个类
Foo
,方法bar()
:
class Foo
{
public:
void bar();
...
};
If you allocate an instance of Foo
on the stack , you use dot notation ( .
) to call the method bar()
: 如果在堆栈上分配
Foo
实例,则使用点表示法 ( .
)来调用方法bar()
:
Foo f;
f.bar();
If you have a pointer to an instance of Foo
, you use the arrow notation ( ->
) to call the method bar()
: 如果你有一个指向
Foo
实例的指针 ,你可以使用箭头符号 ( ->
)来调用方法bar()
:
Foo* pf; // must point to some instance of Foo
pf->bar();
(To complicate things, there are also references , which have value syntax and pointer semantics: if you have a reference to Foo
(eg Foo& f
) you still use dot notation: f.bar();
.) (更复杂的是,还有一些引用 ,它们具有值语法和指针语义:如果你有对
Foo
的引用(例如Foo& f
),你仍然使用点表示法: f.bar();
)
.
is used to access object members, ->
is used to access members through a pointer. 用于访问对象成员,
->
用于通过指针访问成员。 Usually . 通常 。
operator ->
can be overloaded, meaning you can also use it on objects: operator ->
可以重载,这意味着你也可以在对象上使用它:
struct X
{
X* other;
X* operator->() {return other;}
};
X x;
x->other;
In this case, x->other
doesn't reffer to x.other
, but to x.other.other
. 在这种情况下,
x->other
不会对x.other
,而是x.other.other
。 :D :d
No, using .
不,使用
.
to access Objective-C properties is not the same as either ->
or .
访问Objective-C属性与
->
或者不同.
to access struct and class members in C and C++. 在C和C ++中访问struct和class成员。
The Objective-C property accessor works on values of type id
(which is a pointer type), but uses special naming conventions to decide what it actually does. Objective-C属性访问器适用于类型为
id
值(这是一种指针类型),但使用特殊的命名约定来决定它实际执行的操作。 It can directly access a property data member, making it similar to ->
for data member access. 它可以直接访问属性数据成员,使其类似于
->
用于数据成员访问。 Or it can look up special functions for getting and/or setting the property value, in which case it's syntax sugar for a message send. 或者它可以查找用于获取和/或设置属性值的特殊函数,在这种情况下,它是消息发送的语法糖。
Except in the case of operator overloading in C++, ->
is always the same as dereferencing a pointer and then accessing the member referred to. 除了在C ++中运算符重载的情况之外,
->
始终与取消引用指针然后访问引用的成员相同。 a->b
is equivalent to (*a).b
. a->b
相当于(*a).b
。 b
may be a data member for a member function, but the accessed member will have the exact name referred to in b
, not some mutation of it based on any special naming convention. b
可以是成员函数的数据成员,但被访问的成员将具有b
提到的确切名称,而不是基于任何特殊命名约定的某些变异。 If b
names a member function then it may be a virtual function, which has some similarities to, but is not the same as, message sends in Objective-C. 如果
b
命名一个成员函数,那么它可能是一个虚函数,它与Objective-C中的消息发送有一些相似之处,但不一样。 b
may also be an overloaded member function in C++ which has no equivalent in Objective-C. b
也可能是C ++中的重载成员函数,它在Objective-C中没有等价物。
The addition of the .
添加了
.
syntax for accessing object properties in Objective-C violates Objective-C's design principal that new features should look new. 用于访问Objective-C中的对象属性的语法违反了Objective-C的设计原则,即新功能应该看起来很新。 Using
@
, the []
message sending syntax, and the special keywords to define Objective-C objects are examples where Objective-C previously followed this design principal. 使用
@
, []
消息发送语法和定义Objective-C对象的特殊关键字是Objective-C之前遵循此设计原则的示例。
This is objective-c code. 这是客观的代码。
@interface Foo : NSObject
{
NSInteger _a;
}
@property (nonatomaic, assign) NSInteger a;
@end
@implement Foo
@synthesize a = _a;
@end
You know '@synthesize' phrase. 你知道'@synthesize'一词。 @synthesize create bellow codes.
@synthesize创建波纹管代码。
- (NSInteger)a
{
return _a;
}
- (void)setA:(NSInteger)aa
{
return _a = aa;
}
Let's access property a. 让我们访问属性a。
void main()
{
Foo foo = [[Foo alloc] init];
foo.a = 1;
}
Must assigned foo.a as 1. But compiler call as bellow. 必须将foo.a指定为1.但编译器调用如下。
void main()
{
Foo foo = [[Foo alloc] init];
[foo setA:1];
}
foo.a = 1 and [foo setA:1] is same. foo.a = 1和[foo setA:1]相同。 foo.a = 1 calls [foo setA:1].
foo.a = 1次调用[foo setA:1]。
Bellow, Written in C. 贝娄,写于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 also. foo1.a和foo2-> a也是2。 Objectve-C example bellow.
以下是Objectve-C的例子。
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 good day. 祝你有美好的一天。 Thank you.
谢谢。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.