tags:

views:

94

answers:

4

Hey!

I have a class called SparseMatrix which contain a private vector of type Cell.
Cell is a structure that should hold x,y coords and a double value. In addition, I would like that a different class called RegMatix will be able to declare a vector of type Cell also.

this is the struct:

struct Cell {
    Cell(int row,int col, Number value) {
        _cellRow = row;
        _cellCol = col;
        _val = value;
    }
    int _cellRow, _cellCol;
    Number _val;
};

this is sparseMatrix:

class SparseMatrix {

//second, i tried to place the Cell here, but in RegMatrix.cpp Cell was not recognized.

public:
    void Iterator(std::vector<Cell>::const_iterator &startElement,  
                  std::vector<Cell>::const_iterator &endElement) const;
private:
    std::vector<Cell> _matrix;
        //first i tried to place the struct here, but the above line did not recognize
        // Cell. then i placed it above the vector and it worked but RegMatrix.cpp did not recognize it.
};

in RegMatrix.cpp i would like to be able to declare:

std::vector<Cell>::const_iterator start,end;

Eventually i placed it out side the class and it works fine, but is this the correct place for this definition?
And one last question, if I want that other classes would be able to read-only the struct data, is struct the correct structure for Cell or should i create a different class called Cell?

Sorry for the long question, Thank you all!

+2  A: 

If you declare Cell inside of SparseMatrix, you have to scope it to be inside SparceMatrix.

For example:

std::vector<SparceMatrix::Cell>::const_iterator start,end;

You currently have it globally scoped.

As to the best place, if you only use Cell in SparceMatrix, I would declare it in there.

Starkey
A: 

How about defining an abstract parent base class (e.g. AbstractMatrix) that defines a protected Cell struct, and then deriving both SparseMatrix and RegMatrix from AbstractMatrix. In this way both SparseMatrix and RegMatrix can access Cell.

To answer your other question, a struct is fine as long as you do not plan to use polymorphism. You do need to declare the struct properties private in that case and implement accessor methods for the properties.

Ton van den Heuvel
I know inheritance would solve the problem, however in my ex description i'm asked not to use inheritance, rather to write 2 different classes
rob
A: 

If Cell is not accessible only from within SparseMatrix, then it should go outside it.

DeadMG
A: 

Where to put Cell ?

but is this the correct place for this definition?

Despite the two objects (Cell and Matrix) being tied together, they are different objects, and should not need to be linked together. At most, put them in the same namespace.

Note that putting Cell inside SparceMatrix as such:

class SparceMatrix
{
    public :
       // etc.
       class Cell
       {
           public :
               // etc.
       } ;
    private :
       SomePrivateObject m_private ;
} ;

will give the wrong "power" to Cell. In the example above, Cell will have public access to the private m_private object.

Unless you really need such encapsulation violation, make Cell and SparceMatrix separate classes.

Readonly access

if I want that other classes would be able to read-only the struct data, is struct the correct structure for Cell or should i create a different class called Cell?

You need to return a consted-cell.

for example, let's say uou have a method which will return the iterator to the first cell, t should be declared:

std::vector<Cell>::const_iterator SparceMatrix::getFirst() ;

This way, the user should not be able (unless sabotage) to modify the accessed Cell.

The same goes on if you want to return a reference or a pointer to some Cell:

const Cell & SparceMatrix::getSomeCell() ;
const Cell * SparceMatrix::getSomeOtherCell() ;
Cell SparceMatrix::getSomeAnotherCell() ;

The last one will return a copy, which could, or could not be what you want.

To have access to a non-modifiable Cell, consider the const keyword and const iterators.

paercebal