目标c属性到非托管c++
Objective c properties to unmanaged c++
如何从objective c翻译成非托管c++
1property ( nonatomic, assign, getter = isCanceled ) BOOL canceled;
顺便说一下,- isCanceled = false;所以为什么不去赖特呢
property ( nonatomic, assign) BOOL canceled;
与其他操作符在代码的另一部分:
2property ( 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++类通常使用getXyz
和setXyz()
命名约定,这与典型的Objective-C约定仅xyz
和setXyz:
有很大不同,因此将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()
}
}
相关文章:
- 如何导出包含具有"std::unique_ptr"值的"std::map"属性的
- C++概念:如何使用'concept'检查模板化结构的属性?
- 子目录是否继承属性,例如add_definitions,include_directories和父Cmakelist.t
- 通过指向指针数组的指针访问子类的属性
- MSVC是否支持C++11样式的属性而不是__declspec
- QML:修改在不同QML文件(而非main.QML)中定义的子对象的属性
- 类的C++属性似乎已重新初始化
- Qt Quick-如何仅从c++代码与qml属性交互
- 用概念检查属性的类型
- C++如何将同一类的实例作为属性
- 按多个属性排序
- 主函数参数的属性
- "perf_event_attr"结构的"read_format"属性的选项到底是什么?
- C++删除未使用的类属性会导致 std::logic_error
- 无法使用 SWIG 在 Python 中实例化C++类(获取属性错误)
- 使用内存地址访问结构的属性值
- C++调用具有 *this 属性的单个帮助程序函数
- C++ 在堆栈中包含多态属性的类对象存储
- C++ 命名参数习惯用语 - 未设置字符串属性
- 使用 CTRP 时,是否访问访问父构造函数 UB 中的子属性?