Jump to content
  • Advertisement
Sign in to follow this  
WindScar

[C++] Universal get() function.

This topic is 3050 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

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

Share this post


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

Share this post


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

Share this post


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

Share this post


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

Share this post


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

Share this post


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

Share this post


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

Share this post


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

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.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!