Jump to content
  • Advertisement
Sign in to follow this  
VoidSpirit

Assigning property without a setter

This topic is 733 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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?

Edited by VoidSpirit

Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites

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

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!