• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Of Get/Set Functions And Such

 
Greenhorn
Posts: 2
C++ Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Let there be class Foo, which just happens to contain a lot of members.



I'm going to need to make a bunch of get/set functions, so I could change the values of all the members. With all these members, it'd be a pain to write functions for all of them. Is it a good idea (given it's possible) to make a single get/set function for all of the members? If so, then how?
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Magnus Vaikla wrote:With all these members, it'd be a pain to write functions for all of them. Is it a good idea (given it's possible) to make a single get/set function for all of the members? If so, then how?



Considering that all of your members are ints, how about using an array instead? And the members are just different elements of the array? With this option, the get/set will take an additional index parameter.

If that is not possible, meaning you want to keep the original member names, then how about a union with the array? Or just use pointer arithmetic from the first member of the class -- basically use it as an array?

Henry
 
Magnus Vaikla
Greenhorn
Posts: 2
C++ Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Henry Wong wrote:

Magnus Vaikla wrote:With all these members, it'd be a pain to write functions for all of them. Is it a good idea (given it's possible) to make a single get/set function for all of the members? If so, then how?



Considering that all of your members are ints, how about using an array instead? And the members are just different elements of the array? With this option, the get/set will take an additional index parameter.

If that is not possible, meaning you want to keep the original member names, then how about a union with the array? Or just use pointer arithmetic from the first member of the class -- basically use it as an array?

Henry



Well, I have considered the array idea for once, I even googled about it.

This changed my mind. However, the array idea seems to be the only thing that could save me at the moment. Thanks for changing my mind (again) about it.
 
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
And welcome to the Ranch
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic