简体   繁体   English

是否有C ++等同于Objective-C实例和类方法?

[英]Is there C++ equivalent of Objective-C Instance and Class Methods?

I am learning C++ with Xcode (cocos2d-x). 我正在使用Xcode(cocos2d-x)学习C ++。

A regular c++ void method I think is equivalent to an obj-c -void Instance method. 我认为常规的c ++ void方法等效于obj-c -void Instance方法。

Is there is a c++ equivalent of the obj-c +void Class method? 是否有C ++的obj-c + void类方法?

Thanks 谢谢

There is no direct equivalent to class methods in C++; C ++中没有直接等价于类方法的类。 however, in many cases C++ static methods can be used in place of class methods. 但是,在许多情况下,可以使用C ++静态方法代替类方法。

For example, this Objective-C code: 例如,此Objective-C代码:

@interface MyClass : NSObject
{
    int _number;
}
+ (MyClass*)newInstance;
- (void)instanceMethod;
@end

@implementation MyClass
+ (MyClass*)newInstance 
{
    return [[self alloc] init]; 
}

- (void)instanceMethod 
{ 
    _number = 123;
    std::cout << _number;
}
@end

int main(void)
{
    MyClass* foo = [MyClass newInstance];
    [foo instanceMethod];
    return 0;
}

is roughly equivalent to this C++ code: 大致等效于以下C ++代码:

class MyClass
{
    int _number;

public:
    static MyClass* newInstance();
    void instanceMethod();
};

MyClass* MyClass::newInstance() 
{ 
    return new MyClass(); 
}

void MyClass::instanceMethod() 
{ 
    _number = 123; 
    std::cout << _number;
}

int main(void)
{
    MyClass* foo = MyClass::newInstance();
    foo->instanceMethod();
    return 0;
}

That example also illustrates one of the differences between class methods and static methods. 该示例还说明了类方法和静态方法之间的区别之一。

+newInstance will be inherited by subclasses and will always work correctly (it will always return an instance of a subclass). +newInstance将被子类继承,并且将始终正常工作(它将始终返回子类的实例)。 It can also be overridden by a subclass. 它也可以被子类覆盖。

Static methods like MyClass::newInstance() cannot be inherited or overridden. 诸如MyClass::newInstance()类的静态方法不能被继承或覆盖。 It will always return an instance of MyClass . 它将始终返回MyClass的实例。

So when you're porting code between Objective-C and C++ there are cases when you cannot use static methods in place of class methods. 因此,当您在Objective-C和C ++之间移植代码时,有时无法使用静态方法代替类方法。 But for most cases, C++ static methods are a fine replacement. 但是在大多数情况下,C ++静态方法可以很好地替代。

class SomeClass {
public:

    static void someMethod();

};

The equivalent of class methods in C++ would be static functions. C ++中类方法的等效项是static函数。

static return_type function_name(parameters);

Just like in Objective-C, in static functions you cannot reference instance variables (since there's no instance), only static variables. 就像在Objective-C中一样,在静态函数中,您不能引用实例变量(因为没有实例),只能引用static变量。

For example. 例如。

class A{
public:
static void doIt();
};

Then you can call function doIt with: 然后,您可以使用以下命令调用doIt函数:

void main()
{
A::doIt();
}

Objective-C has implicit metaclasses as explored in " What is a meta-class in Objective-C? " article. 正如在“ Objective-C中的元类是什么? ”一文中探讨的那样,Objective-C具有隐式元类。 The full power of metaclasses is not unleashed, however. 但是,并没有释放元类的全部功能。 It would require multiple inheritance support and other technical decisions, as described in famous " Putting Metaclasses to Work " book. 如著名的“使元类投入工作 ”一书中所述,它将需要多个继承支持和其他技术决策。 That was probably too complex for Objective-C authors, so they decided to hide metaclasses in Objective-C. 对于Objective-C的作者来说,这可能太复杂了,因此他们决定在Objective-C中隐藏元类。

Objective-C targets Objective-C runtime (obj.dll or libobjc.dylib), and there were C++ compilers (DTS C++, Direct-to-SOM C++) targetting SOM the same way. Objective-C的目标是Objective-C的运行时(obj.dll或libobjc.dylib),并且有C ++编译器(DTS C ++,Direct-to-SOM C ++)以相同的方式瞄准SOM。 This makes DTS C++ closer to Objective-C than C++ in its design. 这使得DTS C ++在设计上比C ++更接近Objective-C。 I managed to get old Windows DTS C++ compiler running on Windows 8 this way: 我设法以这种方式在Windows 8上运行了旧的Windows DTS C ++编译器:

  1. Download VAC 3.5 fixpak 9 from IBM FTP. 从IBM FTP下载VAC 3.5 fixpak 9 This fixpak contain many files, so you don't even need to full compiler (I have 3.5.7 distro, but fixpak 9 was big enough to do some tests). 该fixpak包含许多文件,因此您甚至不需要完整的编译器(我有3.5.7 distro,但是fixpak 9足以进行一些测试)。
  2. Unpack to eg C:\\home\\OCTAGRAM\\DTS 解压到例如C:\\ home \\ OCTAGRAM \\ DTS
  3. Start command line and run subsequent commands there 启动命令行并在此运行后续命令
  4. Run: set SOMBASE=C:\\home\\OCTAGRAM\\DTS\\ibmcppw 运行:设置SOMBASE = C:\\ home \\ OCTAGRAM \\ DTS \\ ibmcppw
  5. Run: C:\\home\\OCTAGRAM\\DTS\\ibmcppw\\bin\\SOMENV.BAT 运行:C:\\ home \\ OCTAGRAM \\ DTS \\ ibmcppw \\ bin \\ SOMENV.BAT
  6. Run: cd C:\\home\\OCTAGRAM\\DTS\\ibmcppw\\samples\\compiler\\dts 运行:cd C:\\ home \\ OCTAGRAM \\ DTS \\ ibmcppw \\ samples \\ compiler \\ dts
  7. Run: nmake clean 运行:nmake clean
  8. Run: nmake 运行:nmake
  9. hhmain.exe and its dll are in different directories, so we must make them find each other somehow; hhmain.exe和它的dll位于不同的目录中,因此我们必须使它们以某种方式相互找到; since I was doing several experiments, I executed "set PATH=%PATH%;C:\\home\\OCTAGRAM\\DTS\\ibmcppw\\samples\\compiler\\dts\\xhmain\\dtsdll" once, but you can just copy dll near to hhmain.exe 由于进行了几次实验,因此执行了一次“设置PATH =%PATH%; C:\\ home \\ OCTAGRAM \\ DTS \\ ibmcppw \\ samples \\ compiler \\ dts \\ xhmain \\ dtsdll”,但是您可以将dll复制到hhmain附近。可执行程序
  10. Run: hhmain.exe 运行:hhmain.exe

I've got an output this way: 我这样输出:

Local anInfo->x = 5
Local anInfo->_get_x() = 5
Local anInfo->y = A
Local anInfo->_get_y() = B
{An instance of class info at address 0092E318

}

This example does not use metaclasses, it's just for checking DTS C++ compiler. 本示例不使用元类,仅用于检查DTS C ++编译器。

Metaclasses in SOM are explicit, thus so called "class methods" are no more than "instance methods" of metaclass. SOM中的元类是显式的,因此所谓的“类方法”只不过是元类的“实例方法”。 Each object belongs to some class, managed by so called class-object created in runtime. 每个对象都属于某个类,由在运行时创建的所谓的类对象进行管理。 Class-object is itself an instance of another class, named metaclass. 类对象本身是另一个称为元类的类的实例。 Developer of a class can specify metaclass constraint via IDL or DTS C++ pragma. 类的开发人员可以通过IDL或DTS C ++编译指示指定元类约束。

If one needs to call method of known at compile time class, one can just reference class-object and invoke its method just like ordinary object which class-object is in essence. 如果需要在编译时调用已知的方法类,则可以只引用类对象并调用其方法,就像类对象本质上是普通对象一样。 Class-object is being referenced via DLL import/export mechanisms, similar to Objective-C. 类对象通过DLL导入/导出机制来引用,类似于Objective-C。

If one needs to invoke a method of a class that object is belonging to, one needs to invoke somGetClass() to get class-object, typecast it to metaclass type and invoke desired method. 如果需要调用该对象所属类的方法,则需要调用somGetClass()以获得类对象,将其类型转换为元类类型,然后调用所需的方法。 In DTS C++ one should probably not need to do redundant typecasting, but I am not expert in IBM DTS C++ properties. 在DTS C ++中,人们可能不需要做冗余的类型转换,但是我不是IBM DTS C ++属性的专家。

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

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