I don't have a lot of experience in Objective C, but as far as I understand, the translation would be following:
// property ( nonatomic, assign, getter = isCanceled ) BOOL canceled
private:
bool canceled; // inner backing field
public:
bool isCanceled() const // getter has a special name
{
return canceled;
}
void setCanceled(bool newCanceled) // standard setter
{
canceled = newCanceled;
}
For the pointer properties with retain, it's better to use a shared pointer. However, the outer code must adhere to its semantics.
// property ( nonatomic, retain ) Im* img;
private:
// we could use manual retain/release with some kind of shared pointers,
// but an easier way would be to use automatic refcounting
std::shared_ptr<Im*> img; // shared_ptr because retain
public:
Im* getImg() // getter
{
return img.get();
}
void setImg(Im* newImg) // setter
{
img.reset(newImg);
}
The third is the simplest. No need of shared pointers, as no retain
involved.
// property ( nonatomic, readonly ) Parameter* firstPar;
private:
Parameter* firstPar;
public:
Parameter* getFirstPar()
{
return firstPar;
}
// no setter because readonly
etc.
C++ doesn't have the notion of fields, so you have to emulate them a la Java, by manually creating getters and setters.
Edit:
thank to the discussion in the comments, I've corrected the answer by removing the mutex guard. It would be needed if there were atomic
instead.
With atomic
, you would need an additional mutex:
// property ( atomic, assign, getter = isCanceled ) BOOL canceled
private:
bool canceled; // inner backing field
std::mutex canceledGuard; // ensure atomicity
public:
bool isCanceled() const // getter
{
std::lock_guard<std::mutex> lock(canceledGuard);
return canceled;
}
void setCanceled(bool newCanceled) // setter
{
std::lock_guard<std::mutex> lock(canceledGuard);
canceled = newCanceled;
}