Two-dimensional bool matrix.

Hello,

First of all, I'd like to say that I have very little experience in C++. I've done basic programming in several similar languages, but once Pointers come in to play, I find myself missing the insights required to effectively use and understand them.

Now, I'm trying to make a two dimensional matrix of bool variables, which are part of two different classes. As an example, lets call them "row" and "column" classes. Both classes have an array/vector/whatever container with a number of booleans; the size of the container is equal to the number of objects from the opposite class (e.g.: 6 column objects -> every row object has a container of 6 booleans).

I need every bool to be part of both a row object and a column object, so the third bool of the second row object is the same variable as the second bool of the third column object. This seems like a very easy application of pointers/references, both pointing at the same address, but for some reason I can't get it to work.

Could someone provide me with a basic example of both the class/member definition and the methods used to assign the address of one object's member to the other object's member?

Thanks in advance!
So, what you want to do is have and N x M array of bools, N row objects that contain M array of bool* and M column objects that contain N array of bool* ?

Sure you could do that. The code would be my_column[i].ptr[j] = &my_bool_array[i][j]; and swap i with j on one side for rows.

But, why would you want to do that? That is a terrible waste of memory. Now instead of m*n bytes your matrix will take m*n+2*m*n*4 = 9 times more. And that doesn't even give you any useful information..
Thanks for the quick reply!

The point to doing that would be having an easy way to check both horizontal and vertical constraints (basically having a large excel sheet that isn't limited to the basic solver). Having asingle boolean as member of both objects seemed like an easy way to keep track of the impact that changing a single bool value has on both levels. Any suggestions to a leaner way of making such a model are also welcome!

If I understand correctly, your proposed code has a separate 2dimensional bool array which is then accessed by pointers in both objects? I'd rather have the actual booleans being members of one class (rows, as the number of columns is going to be fixed while the rows may vary) and the other class using pointers. The main problem I had was actually returning the address of the booleans so the column objects could access them (though I guess I could easily solve this by making them Public and then using your code?).

You could write a function that generates and returns a row or column object (syntax:my_matrix.getRow(2).bol_ptr[5];//or .getBool(5) ) but I still don't see how that is better than a function that returns just one bool* (syntax: my_matrix.get(2, 5);)

Yes. In the line I posted I assumed public access. You could avoid that with get/set functions, but you don't have to. While encapsulation is generally a good thing, you don't need it everywhere. Especially while you're learning, it just makes things complicated.
Topic archived. No new replies allowed.