obj-c 中的参数通过引用传递
我刚刚做了一项研究,但我遇到了一个问题
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
int r1=40,r2=5;
swap(r1,r2);
NSLog(@" temp is %d",r1);
}
void swap(int r1, int r2)
{
NSLog(@" m i 1st");
int temp;
temp=r1;
r1=r2;
r2=temp;
NSLog(@" temp is %d",r1);
}
,我遇到了 swap
类型冲突;这是正确的做法吗?谢谢!
I just did one research, but I am having a problem
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
int r1=40,r2=5;
swap(r1,r2);
NSLog(@" temp is %d",r1);
}
void swap(int r1, int r2)
{
NSLog(@" m i 1st");
int temp;
temp=r1;
r1=r2;
r2=temp;
NSLog(@" temp is %d",r1);
}
I am getting conflicting type of swap
; is this correct way of doing this? Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您希望交换 r1 和 r2,则必须传递指针或使用 C++ 引用。请注意,使用 C++ 引用将要求您深入研究 Objective-C++,在本例中这意味着将文件命名为
.mm
而不是.m
。然后,使用您的实现之一,如下所示:
输出,无论哪种方式:
If you want
r1
andr2
to be swapped, you either have to pass pointers, or use C++ references. Note that using C++ references will require you to dive into Objective-C++, which in this case means naming your file.mm
instead of.m
.Then, use one of your implementations like so:
The output, either way: