目标c属性到非托管c++

Objective c properties to unmanaged c++

本文关键字:c++ 属性 目标      更新时间:2023-10-16

如何从objective c翻译成非托管c++

1

property ( nonatomic, assign, getter = isCanceled ) BOOL canceled; 
顺便说一下,

- isCanceled = false;所以为什么不去赖特呢

property ( nonatomic, assign) BOOL canceled; 

与其他操作符在代码的另一部分:

2

property ( nonatomic, retain ) Im* img;

,这个结构在c++中只是常量吗?

3

property (nonatomic, readonly)这在c++中是不是像变量

const Parameter* firstPar;

?

以及如何正确翻译第一和第二属性??

对于第一个,在c++中可能是这样的:

class MyClass
{
    bool m_cancelled;
public:
    bool isCancelled()
    {
        return m_cancelled;
    }
    void setCancelled(bool b)
    {
        m_cancelled = b;
    }
};

或者,我发现大多数实现访问器方法的c++类通常使用getXyzsetXyz()命名约定,这与典型的Objective-C约定仅xyzsetXyz:有很大不同,因此将isCancelled()方法命名为getCancelled()可能更有意义。

对于指针类型,这可能会很棘手。没有像Cocoa那样的唯一内存所有权模型,所以很难准确地翻译retain属性应该做什么,但在c++中有这些漂亮的小东西,称为智能指针。了解如何使用智能指针,以及哪种类型的智能指针对其他数据/类类型有用。例如,您可能会发现boost::shared_ptr很有用。

c++中的只读属性可能看起来像这样:

class MyClass
{
    int m_someProp;
public:
    int getSomeProp()
    {
        return m_someProp;
    }
    MyClass(int initialValue) : m_someProp(initialValue)
    {
        // m_someProp can still be altered anywhere within any method within
        // MyClass, but users of MyClass will only have access to the value
        // via getSomeProp()
    }
}