Sign in to follow this  

Problem with reference variables

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

template<class T, int size> class GM_Set
{
    public:
    T &x,&y,&width,&height,&z,&depth;
    T* data;
    GM_Set(): data(new T[size]), x(data[0]), y(data[1]), width(data[2]), height(data[3]), z(data[4]), depth(data[5])
    {}
}


Thats a snippet from my class. What Im doing is basically making code names for the first six elements of data but for some reason its not working. It crashes when I try to access set.x but allows me to access data[0].

Share this post


Link to post
Share on other sites
First off, this is a bad, bad idea. As to your actual problem, constructors for member variables are called in order of their declaration in the class; not in the order in the member initialization list. So x is being assigned to data[0] before data gets populated, and so is likely referencing a bad memory address. One way to address this would be to move the data declaration before the reference declarations. Also, you might want to try turning the warning level on your compiler up. Many compilers will give you a warning if your member initialization list is different order than your member variable declarations.

Share this post


Link to post
Share on other sites
Well, I guess I wanted the class to be able to be used for more things than just 2d and 3d boxes. I wanted the class to be able to possibly hold more than 6 elements and I figured being able to iterate through the elements would make certain algorithms easier.

I wanted reference code names for a bit of simplicity in the case that the class was being used to represent a box.

edit:
Thx Crane for the answer and the warning. I'll watch this code carefully if I decide to use it.

Share this post


Link to post
Share on other sites
Quote:
Original post by ForeverNoobie
Well, I guess I wanted the class to be able to be used for more things than just 2d and 3d boxes. I wanted the class to be able to possibly hold more than 6 elements and I figured being able to iterate through the elements would make certain algorithms easier.

I wanted reference code names for a bit of simplicity in the case that the class was being used to represent a box.


I think this is a case of over zealous design. A box should behave like a box, and an elephant should behave like an elephant. I don't think you would want to design a boxephant, however in the event that you want to be a mad scientist you could use multiple inheritance, but that probobly is not a good idea either.

Share this post


Link to post
Share on other sites

This topic is 3628 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.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this