Sign in to follow this  

Question about arrays in classes.

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

Can I have an array that serves as a member of a custom class that is of a size determined by the custom class's constructor? The following does not compile, for example:
[code]class tileGrid
{
public:
const int X;
const int Y;
tile tiles[X][Y];
public:
tileGrid(const int XX,const int YY);
};

tileGrid::tileGrid(const int XX,const int YY) : X(XX) , Y(YY)
{
// Stuff
}[/code]But I was wondering if I could do something similar?

Share this post


Link to post
Share on other sites
[quote name='Ruzhyo2000' timestamp='1298189512' post='4776602']
Can I have an array that serves as a member of a custom class that is of a size determined by the custom class's constructor? The following does not compile, for example:
[code]class tileGrid
{
public:
const int X;
const int Y;
tile tiles[X][Y];
public:
tileGrid(const int XX,const int YY);
};

tileGrid::tileGrid(const int XX,const int YY) : X(XX) , Y(YY)
{
// Stuff
}[/code]But I was wondering if I could do something similar?
[/quote]


This could work:

[code]class tileGrid
{
public:
const int X;
const int Y;
tile *tiles;
public:
tileGrid(const int XX,const int YY);
};

tileGrid::tileGrid(const int XX,const int YY) : X(XX) , Y(YY)
{
tiles = new tile[XX*YY];
}[/code]


assainator

Share this post


Link to post
Share on other sites
The space of an arrary need to be calculate at compile time, replace it with vector[img]http://public.gamedev.net/public/style_emoticons/default/wink.gif[/img]

Share this post


Link to post
Share on other sites
[quote name='Baesky' timestamp='1298201255' post='4776628']
The space of an arrary need to be calculate at compile time, replace it with vector[img]http://public.gamedev.net/public/style_emoticons/default/wink.gif[/img]
[/quote]

Agreed, use Vectors. 2D vectors may look butt ugly, but they are easy to use.

Share this post


Link to post
Share on other sites
You don't have to name your constructor arguments differently to your member variables.
This is perfectly fine and guaranteed to do the right thing:[code]tileGrid::tileGrid(int X, int Y) : X(X) , Y(Y)[/code]

Share this post


Link to post
Share on other sites
Thanks, I'm using a vector now. The only reason I wasn't doing it in the first place was because, as someone mentioned, they are very ugly lol. I get confused sometimes just setting them up and really didn't want to use the pop/push_back() functions or change the size of it at all. Is there any way to do 3D vectors? If I wanted to access an element in a 3D array, I would do something like this: [code]myArray[i][j].member = 0;[/code] but now, with a vector, I am doing this: [code]myVector[(j*X)+i].member = 0;[/code] where X is the width of the mock 3D vector. I rather just be able to do it the same way I do with the 3D array, as this would really draw down on the "confusion level" of some of my code.

[quote name='iMalc' timestamp='1298225800' post='4776739']
You don't have to name your constructor arguments differently to your member variables.
This is perfectly fine and guaranteed to do the right thing:[code]tileGrid::tileGrid(int X, int Y) : X(X) , Y(Y)[/code]
[/quote] Question about that, actually: what if I want to do stuff with either the data member X or the parameter X inside the constructor? I figure this.X will refer to the member X, but will just regular X refer to the member or the parameter? I know it wouldn't matter in this one because they are both constants, but I am just wondering for future reference.

Share this post


Link to post
Share on other sites
[quote name='Ruzhyo2000' timestamp='1298233431' post='4776780']
what if I want to do stuff with either the data member X or the parameter X inside the constructor?
[/quote]
I was curious about this so I wrote a test to see what the behavior was:

[code]struct alpha
{
int x;
alpha(int x) : x(x) {
x = 10;
}
};

int main() {
alpha beta(5);
return beta.x;
}[/code]

When I run this in the Visual C++ 2010 debugger the debug window says the returned value is 5. I'm almost positive this is what the standard dictates and it makes sense if you think about it. The initialization list syntax only works if the variable 'a' in 'a( b )' is the member variable. 'b' has no such requirement so the normal rules about identifier resolution apply and the compiler uses the most local variable just as it does in a normal code block. I bet you could use a global variable in the initialization list if you used the scope resolution operator ( [url="http://en.wikipedia.org/wiki/Scope_resolution_operator#C.2B.2B"]http://en.wikipedia....perator#C.2B.2B[/url] ).

Someone who knows where to look in the standard can correct me if I'm wrong.

Share this post


Link to post
Share on other sites
[quote name='Ruzhyo2000' timestamp='1298233431' post='4776780']
Question about that, actually: what if I want to do stuff with either the data member X or the parameter X inside the constructor? I figure this.X will refer to the member X, but will just regular X refer to the member or the parameter? I know it wouldn't matter in this one because they are both constants, but I am just wondering for future reference.
[/quote]

this->X will refer to the member and X to the the most local variable named X (this can be a parameter or a locally declared variable)

consider for example:

[code]
class test {
private int x,y;
void testFunction(int x) {
int y=5;
//now this->x and this->y refers to the uninitialized member variables, x to the parameter and y to the local variable (with the value 5)
}
[/code]

Share this post


Link to post
Share on other sites

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