Don’t do Java bean properties in C++

The reason why Java has the bean API is to ensure that the caller does not delete the object when setting it.

void setX(String val){

if (val == null) return;

this.x = val;

}

This is not necessary in C++, since the parent object owns the memory of the child object.  The equivalent C++ code would be simple

o.x = val;

If you want to do validation (see my earlier posts about regex validation) use a subclass that does the validation in its constructor and assignment operator.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.