441 views
in GUI Development by
I ran into problems recently when I was using custom classes.

In essense I had a class A with a few properties of more basic types (e.g. bool, int etc).

I then had a class B, with a property of type class A.

Class B had an autoobject to make it stay around, but when it was initialized it didn't seem to call the init function on the property of type class A, and so this failed with either a hard fault or data corruption at run time.

The workaround that I discovered was to have an instance of the class A rather than a property of that type (but the original way I did it compiled fine both in Embedded Wizard Studio and the device C code, so it seemed like it should work?)

The downside of the workaround is I couldn't then have property observers on that object without creating an extra property just as a flag that the data in the object had been updated.

Is there a better way to be doing this?

1 Answer

0 votes
by

Hello,

have you specified some initialization expression for the propertry in class B? If not, the property is initialized per default with null. Accessing it will cause an access violation.

Best regards

Paul Banach

by
Hi Paul,

What would be the syntax would be for initializing a property of type class?

If I create an autoobject of the class, I can use that as a default value (but do I have to create an autoobject for every property that uses that class, or will it just be used at initialization time and each have their own memory anyway?)

Nathanael
by

Hello Nathanael,

Maybe there is a misunderstanding. The initialization value of a property (its default value) is determined when you edit the class containing the property. How you add and initialize properties is explained in the article Property.

If you have a property declared with a class as its type, the property will be able to store a reference to an instance of that class. Every object matching the specified class can thus be stored in this property. Several properties refering to the same instance will share the same reference. Concrete, if you have two properties every initialized with the same autoobject, both properties refer to exact this autoobject instance.

I hope it helps you further. Concering your question "If I create an autoobject of the class, I can use that as a default value (but do I have to create an autoobject for every property that uses that class, or will it just be used at initialization time and each have their own memory anyway?)" I don't understand what you intend to do.

Best regards

Paul

by

That is useful. The key part that I didn't understand is that the default value of a property of type class stores a reference to an instance of that class, and so you must have an instance/object somewhere to pass in.

For an bool property, translating to C/C++ is like:
bool foo = [default value = true/false];
but for a class property translating to C/C++ is like:
CustomClass & a = [default value = object which must be declared somewhere else];

by

Yes. It is like a pointer to an object. Initializing the 'pointer' with null means no object. Several 'pointers' can refer the same object sharing it in this manner.

Besides those 'pointers' you can also embed an object directly inside a class. At the runtime the instance of the class will contain the embedded object. If you are interested in this feature see the article embedded objects.

Best regards

Paul Banach

Ask Embedded Wizard

Welcome to the question and answer site for Embedded Wizard users and UI developers.

Ask your question and receive answers from the Embedded Wizard support team or from other members of the community!

Embedded Wizard Website | Privacy Policy | Imprint

...