Setting properties of an object stored as a member of another object

B

Bora Eryilmaz

Which option illustrated below is better and why?

The first option would require that the user has knowledge of the MyData
object and that he goes through two object layers.

The second option is more direct, but if one were to set all of MyData
object's properties this way, it would be a lot of methods to
write/duplicate, especially if MyData object already had its "set" functions
defined.

What is your opinion?


MyObject obj = new MyObject(); // This object has a private member that
stores an array of MyData objects
MyData *data;

// First option
data = obj.getData(1); // Get the first data object stored in obj.
data->setValue(10); // Set the value to 10

// Second option
obj.setDataValue(1, 10); // Set the value of first MyData object to 10.
 
V

Victor Bazarov

Bora said:
Which option illustrated below is better and why?

The second is better because it provides higher abstraction.
The first option would require that the user has knowledge of the MyData
object and that he goes through two object layers.

The second option is more direct, but if one were to set all of MyData
object's properties this way, it would be a lot of methods to
write/duplicate, especially if MyData object already had its "set" functions
defined.

What is your opinion?


MyObject obj = new MyObject(); // This object has a private member that
stores an array of MyData objects
MyData *data;

// First option
data = obj.getData(1); // Get the first data object stored in obj.
data->setValue(10); // Set the value to 10

// Second option
obj.setDataValue(1, 10); // Set the value of first MyData object to 10.

V
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top