Solved NewObj error: no suitable conversion

This must be incredibly basic and I'm sorry for not being able to figure this out myself.

I'm working in C++ on 2023.2.

I'm creating an ObjectData plugin. I created a utility class to help me manage some data and I'd like to create an instance of this class inside my ObjectData. In terms of memory allocation for classes the documentation says right here:

Any class allocation (except for system or library specific code) must be done using NewObj() and DeleteObj().

So I tried using NewObj like this:

MyClass* myClass = NewObj(MyClass);

But this won't let me compile due to the following error:

cannot convert from 'maxon::ResultMemT<T *>' to 'MyClass *'

Also this error:

no suitable conversion function from "maxon::ResultMemT<MyClass *>" to "MyClass *" exists

In the documentation of NewObj it says:

Usage: MyType* x = NewObj(MyType, optional constructor parameters);

What am I missing?

Just in case, this is my actual utility class:

#include "c4d.h"

class MainControllerHandleInformation // Wanted to use a struct even but changed it to class while fiddling with the error.
{
public:
    MainControllerHandleInformation() = default;
    maxon::Pointer<BaseObject> MainController; // Should I even use maxon::Pointer or just BaseObject*?
    Bool IsLeftHandle;
    maxon::Pointer<BaseObject> ThisArm;
    maxon::Pointer<BaseObject> OtherArm;
};

And this is actually the code to initialize it:

MainControllerHandleInformation* handleInformation = NewObj(MainControllerHandleInformation);

Hi @CJtheTiger,

Thanks for reaching out to us. You are doing things right and are only missing the error handling macro iferr_return, which does extract class pointer MyClass* from the ResultMem object. You can find the example class initialization code snippet here.

In your specific case the initialization line would look like:

maxon::Result<void> Foo()
{
  iferr_scope;  // do not forget iferr_scope, to make error handling work

  MyClass* myClass = NewObj(MyClass) iferr_return;

  return maxon::OK;
}

I personally would suggest this manual, as it gives a detailed overview of error handling macros that are commonly used within our sdk frameworks.

Let me know if you have any further questions.

Cheers,
Ilia

MAXON SDK Specialist
developers.maxon.net

Hi @CJtheTiger,

Thanks for reaching out to us. You are doing things right and are only missing the error handling macro iferr_return, which does extract class pointer MyClass* from the ResultMem object. You can find the example class initialization code snippet here.

In your specific case the initialization line would look like:

maxon::Result<void> Foo()
{
  iferr_scope;  // do not forget iferr_scope, to make error handling work

  MyClass* myClass = NewObj(MyClass) iferr_return;

  return maxon::OK;
}

I personally would suggest this manual, as it gives a detailed overview of error handling macros that are commonly used within our sdk frameworks.

Let me know if you have any further questions.

Cheers,
Ilia

MAXON SDK Specialist
developers.maxon.net

Hi @i_mazlov,

no wonder I felt like I was missing something! And now that the iferr_scope is declared I have to bubble it up all the way and that's how I'll get proper error handling. Sweet!

To be honest I read about error handling before but was hoping I could skip it during the prototyping phase. But now that I gotta do it, once I'm out of prototyping it'll even be a lot more stable. Plus I also gotta learn about References now which is probably a good idea.

2c37be53-a3ce-4695-afdb-ab8d3410d19b-image.png

Thanks for the quick help! Have a nice rest of the day!

Best wishes,

CJ