Question about arrays in classes.

Started by
6 comments, last by SimonForsman 13 years, 1 month ago
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:
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
}
But I was wondering if I could do something similar?
Advertisement

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:
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
}
But I was wondering if I could do something similar?



This could work:

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];
}



assainator
"What? It disintegrated. By definition, it cannot be fixed." - Gru - Dispicable me

"Dude, the world is only limited by your imagination" - Me

The space of an arrary need to be calculate at compile time, replace it with vectorwink.gif

The space of an arrary need to be calculate at compile time, replace it with vectorwink.gif


Agreed, use Vectors. 2D vectors may look butt ugly, but they are easy to use.
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:tileGrid::tileGrid(int X, int Y) : X(X) , Y(Y)
"In order to understand recursion, you must first understand recursion."
My website dedicated to sorting algorithms
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: myArray[j].member = 0; but now, with a vector, I am doing this: myVector[(j*X)+i].member = 0; 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.


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:tileGrid::tileGrid(int X, int Y) : X(X) , Y(Y)
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.

what if I want to do stuff with either the data member X or the parameter X inside the constructor?

I was curious about this so I wrote a test to see what the behavior was:

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

int main() {
alpha beta(5);
return beta.x;
}


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 ( http://en.wikipedia....perator#C.2B.2B ).

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

C++: A Dialog | C++0x Features: Part1 (lambdas, auto, static_assert) , Part 2 (rvalue references) , Part 3 (decltype) | Write Games | Fix Your Timestep!


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.


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:


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)
}
[size="1"]I don't suffer from insanity, I'm enjoying every minute of it.
The voices in my head may not be real, but they have some good ideas!

This topic is closed to new replies.

Advertisement