Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

aaron_ds

Member aliases

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

I have a point class that I use to store three floats in (x,y,z). However, I would also like to be able to call them by different names for instance (a,b,c) or (r,g,b). Is this impossible using C++ or am I just missing something obvious? Thank you

Share this post


Link to post
Share on other sites
Advertisement
It''s not standard and probably not good practice, but you can do something like with MSVC:

class Point {
public:
union {
struct {
float x;
float y;
float z;
};
struct {
float a;
float b;
float c;
};
struct {
float r;
float g;
float b;
};
};
};

Share this post


Link to post
Share on other sites
I want to use the class for a variety of situations. Otherwise, I would have 3 classes with the only difference between them being the names of the members.

Share this post


Link to post
Share on other sites
quote:
Original post by aaron_ds
I want to use the class for a variety of situations. Otherwise, I would have 3 classes with the only difference between them being the names of the members.


This is probably not what you want to hear...

I don''t think that is a very good reason. There are many differences between a color and a point. The fact that their implementations are similar now is not a good reason for making them the same class. What happens when you start adding member functions? You are giving up a lot of flexibility for a tiny bit of convenience.

On the other hand, if all you are doing is creating a class that holds three floats, then it shouldn''t matter what they are named because the class is not a point and it is not a color, it is just something that holds three floats.

Share this post


Link to post
Share on other sites
Use references! Just have some public member references and point them to whatever you want at construction time.

Edit: However, I agree with the above poster.

[edited by - dcosborn on March 21, 2004 2:39:14 PM]

Share this post


Link to post
Share on other sites
Using references as member aliases is a bad idea. That will bloat the size of the class for no good reason. It also subverts const-correctness.

Share this post


Link to post
Share on other sites
I don''t understand how references subvert const correctness. If you need constant references, just make them constant. If you mean that non-const references can be made to point constant variables, then thats not really an issue since you can do the same sort of thing with const_cast if you want, but any sensible person would avoid such a thing.

As for code size, I''d hope that a compiler would optimize it away.

Share this post


Link to post
Share on other sites
Well, I Have added member functions, mst of them being operators. And I agree that it is not the right way to do things. (the reason why im asking). Perhaps inheritance would be a better solution? Have a generic class which holds 3 floats and then derive a point, etc from there? But that beingsup the question of renaming still. Can I alias inherited members?

Share this post


Link to post
Share on other sites
Yes indeed you can make aliases for inherited members using references. The question is, is that the best solution?

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!