简体   繁体   English

关于可可C风格功能的一些愚蠢的问题

[英]Some dumb questions about Cocoa C-style functions

In spite of I'm developing for years in Cocoa, I never have to develop a kind of C-style library for working on non-objects vars. 尽管我在Cocoa中开发了多年,但我从未开发一种C风格的库来处理非对象var。

What I call "C-style functions" is, for example: 我所谓的“ C样式函数”例如:

float myCStyleFunction(float var1, float var2);

Knowing Objective-C is a descendant of C, I was trying to make a real C function. 知道Objective-C是C的后代,所以我试图制作一个真正的C函数。 But some simple things are not permitted by the compilator. 但是编译器不允许一些简单的事情。

First the pass-by-reference : 首先通过引用:

void makeSomethingWithImages(CGImageRef & im1, CGImageRef & im1);

This do not work. 这行不通。 Compilator says Parameter name omitted . 编译器说Parameter name omitted Is there a way to pass-by-reference my CGImages? 有没有一种方法可以通过引用传递我的CGImage?

Secondly, defaults parameters : 其次,默认参数:

void makeSomethingWithImages(CGImageRef im1, CGImageRef im1, float tolerance) { ... }

void makeSomethingWithImages(CGImageRef im1, CGImageRef im1) {
    makeSomethingWithImages(im1, im2, 0);
}

Error : Conflicting types for 'makeSomethingWithImages' 错误: Conflicting types for 'makeSomethingWithImages'

Is there a way to have some defaults parameters? 有没有办法设置一些默认参数?

And the subsidiary question : if Obj-C is a C descendant, why these examples do not work? 附带的问题是:如果Obj-C是C的后代,为什么这些示例不起作用?

通过引用传递的是C ++而不是c,因此默认参数和函数重载也是如此,尽管clang为函数重载向c添加了扩展名,但是有附加的语法。

Pass by reference shall be using the- * notation( pointer to the Address ), & is a " Value at Address ", CGImageRef is defined as CGImage *CGImageRef; 通过引用传递应使用- *表示法( 指向地址的指针 ), &是“ 地址处的值 ”, CGImageRef定义为CGImage *CGImageRef; in CGImage.h class so the compiler Err.. CGImage.h类中,所以编译器Err ..

No, you can't use by-reference passing mechanism, nor default value for arguments in C. If Obj-C is a descendant of C, this means that legal C code is accepted by Obj-C compilation, not the converse. 不,您不能使用按引用传递机制,也不能使用C中参数的默认值。如果Obj-C是C的后代,则这意味着合法的C代码被Obj-C编译接受,而不是相反。

But you've made a mistake, by-reference and default values are not available in Objective-C. 但是您犯了一个错误,在Objective-C中没有按引用和默认值。 Both of these constructions are C++ ones, not C ones. 这两种构造都是C ++,而不是C。 There exists a mix of Objective-C and C++, it is called Objective-C++. 存在Objective-C和C ++的混合,称为Objective-C ++。

Both C++ and Objective-C are descendant of C, so the mix. C ++和Objective-C都是C的后代,因此混合使用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM