[C++] Universal get() function.

Started by
33 comments, last by WindScar 14 years, 2 months ago
Question in comment inside code:

class guy
{
   public:
   foo(int _a)
     : a(_a)
   {
   }

   get() //How to write that function so main() will input like shown below?

   private:
   int a;
}

int main()
{
   guy(1553);
   std::cout << guy.get(a); //Note it's not geta(), it's get(a), should work for any get(variable_name)
   return 0;
}


Desired input:
Quote:1553
Please point my english mistakes everytime you can.
Advertisement
You by and large can't. Pointer to members can maybe work here, but with a being private, you're pretty much out of luck.
C++ has no built in mechanism for this. There are various ways to emulate this behavior, but the details depend on what your exact needs are.

For more info on the subject, Google 'c++ reflection'.
Doesn't a generic get() method defeat the purpose of making members private? If I want to change the internal implementation of my class in the future, code that uses get() would break, even if I leave my public interface unchanged.
You can't really do it in general, and you don't want to.

'private' exists for a reason. If you are trying to work around it, you haven't understood the reason properly.
Thanks for the answers, but I do have reasons. The get() is just an example so you know what I want, so I don't have to explain the entire reason. If I can manage to make it work, I can also:

1. Simplify the parameters call when many variables have default values.
EG: Being able to call: bullet(speed=4) instead of bullet(0,0,4), when the middle 2 variables are, for exemple, it's position and their defaults are 0.

2. Be able to make a sort function to my container that sorts it's contained objects from one of it's variables, example:

container<bullet> bullets(bullet(0,0,3), bullet(20,1,4), (4,3,1));
bullets.sort(speed,1); //sort by speed asc
bullets.sort(x,0); //sort by x desc

3. Be able tell a function what object to create. Example:

class sweetiesFabric { blabla };
class chokobunny { blabla };
class goodcake { blabla };
sweeties_fabric_obj.create(chokobunny);
sweeties_fabric_obj.create(goodcake);

So I tell the fabric both to create a chokobunny object and a goodcake object. I suppose it can be done with a switch function, but then the function would have to be updated for every new class created and I suppose that goes against the good coding rules.

...

And, also, the get() function proposed won't violate the private as it is supposed to return just a copy of the variables.
Please point my english mistakes everytime you can.
C++ doesn't work like that. You can build things that emulate that, but it will be long and painful and at the end it may be no cleaner than the alternative.
But why it doesn't? Mysql for example got "sort obj by var ASC" and is great, so I don't se reasons for ommiting that possibility.
Please point my english mistakes everytime you can.
Quote:Original post by WindScar
But why it doesn't? Mysql for example got "sort obj by var ASC" and is great, so I don't se reasons for ommiting that possibility.


Speed. C++ is build for speed. Reflection systems always require some kind of class metadata and have an effect on performance.
Will Miller | Game Designer | Big Huge Games
Well I see, but what are the best solutions in your opnion to the cases pointed in my last post (edited)?
Please point my english mistakes everytime you can.

This topic is closed to new replies.

Advertisement