Array within a class

Started by
10 comments, last by Alpha Brain 17 years, 7 months ago
hello. I'm pretty new with classes. I'm pretty used with c-structures. And I'm trying to make this code work using my knowledge from structs: #include <iostream> using namespace std; class arrayTest { public: int data[8]; void printArray(); }; void arrayTest::printArray() { for(int i=0; i<8; i++) { cout << data << endl; } } int main(void) { arrayTest array; array.data[3] = { {{0,1,2,3,4,5,6,7}}, {{0,1,2,3,4,5,6,7}}, {{0,1,2,3,4,5,6,7}} }; // unlike a struct, it doesn't work. return 0; } Technically I would like to initialize an array similar to the following process; /****************** IT IS WORKING WITH A STRUCT ****************/ typedef struct { data[8]; }arrayTest; arrayTest array[3] = { {{0,1,2,3,4,5,6,7}}, {{0,1,2,3,4,5,6,7}}, {{0,1,2,3,4,5,6,7}} }; /****************************************************************/
Advertisement
I don't believe you can actually set things like that, you have to do it line-by-line, element by element. In the case of 1-7, a simple for-loop would be able to do it with minimal effort.

for(int i = 0; i < 7; i++)
array.data = i+1;

Also, your printArray function is going outside the bounds of the array. It should be this:

for(int i=0; i<7; i++)
cout << data << endl;
actually I want an array of array like on my last exemple using a struct.

Then you need two loops instead of one; one to cycle thru each of your arrays, and the other to populate the class array with elements.

#include <iostream>

using namespace std;

class arrayTest
{
public:
int data[8];
void printArray();
};

void arrayTest::printArray()
{
for(int i=0; i<7; i++)
cout << data << endl;
}

int main(void)
{
// your array
arrayTest array[3];

// two loops; one to cycle thru each of your 3 arrays,
// and a 2nd to populate each element of the array in
// your class
for(int i = 0; i < 3; i++)
for(int j = 0; j < 7; j++)
array.data[j] = j+1;

// a loop to print all the elements in your array, seperated
// by an endl.
for(int i = 0; i < 3; i++)
{
array.printArray();
cout << endl;
}

system("PAUSE"); // Just to prompt the user to press a key. I use
// Dev-C++, so this keeps it from vanishing instantly
return 0;
}
but can we initialize an array from a class using brackets?
you can initialize it the way you were doing it:

int main(void) {  arrayTest array[3] = {    {{0,1,2,3,4,5,6,7}},    {{0,1,2,3,4,5,6,7}},    {{0,1,2,3,4,5,6,7}}  };  return 0;}


You can initialize structures witht he bracket approach. For example if you had a structure like:

struct s { int x; float y; }

you can do:

s anS = { 3, 7.6f };

and then

s.x == 3 and s.y == 7.6
[size=2]aliak.net
Hmmm...I didn't think you could do that. But what happens if you have multiple arrays in your class? Does it go in the order listed in the class specification? I'm just curious how this happened, since there's no constructors or anything.
Quote:But what happens if you have multiple arrays in your class? Does it go in the order listed in the class specification?


Yep:

struct structure {	int a1[2];	int a2[2];	int a3[2][2];};int main(void){	structure s = {		{ 1, 2 },		{ 3, 4 },		{ { 5, 6 }, { 7, 8 } }	};}


Quote:I'm just curious how this happened, since there's no constructors or anything


That's why it *can* happen. The struct above is a POD type. Read the link and you'll understand why it works.
[size=2]aliak.net
Quote:Original post by Alpha Brain
int main(void)
{
arrayTest array;

array.data[3] =
{
{{0,1,2,3,4,5,6,7}},
{{0,1,2,3,4,5,6,7}},
{{0,1,2,3,4,5,6,7}}
}; // unlike a struct, it doesn't work.

return 0;
}

Technically I would like to initialize an array similar to the following process;

/****************** IT IS WORKING WITH A STRUCT ****************/
typedef struct
{
data[8];
}arrayTest;

arrayTest array[3] = {
{{0,1,2,3,4,5,6,7}},
{{0,1,2,3,4,5,6,7}},
{{0,1,2,3,4,5,6,7}}
};

/****************************************************************/


If you wanted to do it "a similar way" to how you do it with a struct, why is the syntax you're trying for the initialization totally different?

Hints:

- In C++, classes and structs are really the same thing. The keywords are basically syntactic sugar for each other. The only thing that changes is the default "protection" (public/private/protected) for inherited bases and for the class/struct body.

- In C++, you can't have implicit "int" types for things any more. And you shouldn't use the "typedef struct idiom" (although it will still work), because it's not necessary - you don't ever need 'struct' when you declare a variable of the type, just as you wouldn't need 'class'.

- Speaking of which: classes and structs in C++ - and structs in C - declare types. If you say "arrayTest array;", then array is already an arrayTest, and it is too late to make it an array of arrayTests. Similarly, 'array.data[3]' is element 3 of the 'data' member of the 'array' arrayTest instance, exactly as it would be with a struct.

- In summary, you can initialize the class (as long as it's a POD type; see IFooBar's link) just the same way (of course, in C++ the POD type restriction applies to structs as well. In C, you can ONLY make structs that C++ would consider POD types, because the things that cause something to not be a POD are all C++-exclusive), and you're just confused about what you made your class represent (it represents just one row, not the whole 2D grid). Of course, *assigning* later with {} syntax doesn't work - again, just as it wouldn't work with a struct. It is only an initialization syntax.

- Please forget trying to do it yourself - even people who can get everything right the first time don't do it themselves, because it is done - in a few standardized flavours according to your needs. In your case (where you have a specific size in mind ahead of time, but you want the array to behave in an object-like way so that e.g. copying it works in a way you'd expect, and so that the size can be checked and you don't have pointer-decay nastiness going on), what you want is boost::array. Oh, and trying to add printing functionality to a class that represents an array is probably a bad idea in terms of design. That's not really how OO works, at least not in C++.
Quote:Original post by Alpha Brain
	arrayTest array;	array.data[3] = {            {{0,1,2,3,4,5,6,7}},            {{0,1,2,3,4,5,6,7}},            {{0,1,2,3,4,5,6,7}}};                            // unlike a struct, it doesn't work.


It wouldn't work with a struct either: you are trying to assign an array literal, which is not possible. Initializing one is.
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan

This topic is closed to new replies.

Advertisement