Extending NS::Object removes constructor

Hi!

I'm currently trying to convert this Objective-C example project ( https://developer.apple.com/documentation/metal/performing_calculations_on_a_gpu?language=objc ) to one using the metal-cpp wrapper.

However when I make the MetalAdder class extend NS::Object (just like in the original codebase) it removes my constructor.

class MetalAdder : public NS::Object{ ... } is what I have.

When I instantiate this MetalAdder class as:

MetalAdder adder;
adder.initWithDevice(device);

or

auto adder = NS::TransferPtr(new MetalAdder);

I get the error Call to implicitly-deleted default constructor of 'MetalAdder'.

Is there something I'm doing wrong? Should I instantiate in a different way or should my MetalAdder class just not extend the NS::Object class?

Thanks in advance!

Answered by Graphics and Games Engineer in 744197022

Hi Jerne,

NS::Object is a wrapper around NSObject that helps metal-cpp objects communicate with the Objective-C runtime. The wrapper imposes some restrictions typical of Objective-C classes on its subclasses, such as preventing allocating the object on the stack or using a C++ destructor.

In the original Objective-C sample, the MetalAdder class extends NSObject because it is required by the language. In your case, you are not bound by the same requirement.

For simplicity, I would recommend you handle MetalAdder as you would any other C++ class (or struct) and not place it in the Objective-C class hierarchy. This will enable it to behave like any other C++ object once instantiated, allowing you to allocate it on the stack and having a typical C++ destructor.

Accepted Answer

Hi Jerne,

NS::Object is a wrapper around NSObject that helps metal-cpp objects communicate with the Objective-C runtime. The wrapper imposes some restrictions typical of Objective-C classes on its subclasses, such as preventing allocating the object on the stack or using a C++ destructor.

In the original Objective-C sample, the MetalAdder class extends NSObject because it is required by the language. In your case, you are not bound by the same requirement.

For simplicity, I would recommend you handle MetalAdder as you would any other C++ class (or struct) and not place it in the Objective-C class hierarchy. This will enable it to behave like any other C++ object once instantiated, allowing you to allocate it on the stack and having a typical C++ destructor.

Extending NS::Object removes constructor
 
 
Q