Advertisement

Assigning property without a setter

Started by October 19, 2016 08:17 PM
3 comments, last by VoidSpirit 8 years, 1 month ago

Good day!

I have an application class with property which is object too:

// C class

class Foo

{TName * name; // TName is class

TName * get_name(); // for registering as getter
};

I'm registering a getter for 'name':

TName & get_name();

Class TName has 'opAssign' methods for different types.

Now I'm trying to create a Foo class object and assign 'name' value using TName assign methods:

Foo foo();

foo.name = "TEST"

foo.name = anything else...

And I have an error 'Property 'name' has no setter'.

But next code is legal:

foo.get_name() = "TEST" // here working TName.opAssign

I want keep a TName assigning functionality without complicated setters in Foo class. How can I legally write it?

You have only written a property "getter." You need to write a property "setter" to get the name property to handle an assignment operator.

http://www.angelcode.com/angelscript/sdk/docs/manual/doc_script_class_prop.html


void set_name(TName value);
Advertisement

to Jason Goepel:

Yes, I know. But property type (class TName in example) has some different assigning methods, and in this case I must write same number of setters. Or write setter with variable type parameter. I don't want duplicate assigning methods by setters. How can I write variable type setter, or solve it by other way?

If your class is registered in a way as straightforward as you present it, the most reasonable thing to do would be to register the property as a non-virtual property. Getters and setters are only useful if it is unsafe to access the variable directly. If a setter is necessary because a change to the property may have side effects, then, well, you need a setter. AngelScript supports variable type functions but type compatibility is not verified at compile time, i.e. the function would accept arguments of any type. I don't recommend this for cases where the number of supported types is finite, but if it works for your purposes, you can read about it here. Otherwise you will unfortunately have to register multiple setters. That said, I'm not sure why you're so opposed to this idea, as you can do this without much work using a couple of C++ templates. Our project does this for a couple of methods with >10 overloads.

to Sir Ementaler

Thank you for detailed answer.

In my task property is nested object, and immediate changing property value is not needed, only assign some value to this nested object using it's overloaded assigning methods. I.e. there are legal semantics:

foo.name.opAssign("TEST") // used getter for 'name' and overloaded assign

and

foo.get_name()="TEST" // getter for 'name' used directly, overloaded assign used implicitly

but not

foo.name="TEST" // expected implicit using getter and overloaded assign but...

I tried to reduce a redundant work and to avoid duplicating functionality. I think I should search other way

This topic is closed to new replies.

Advertisement