S
SpOiLeR
Hello!
I have a matrix class like this:
class MyObject;
// MyMatrix is contains MyObjects
class MyMatrix {
public:
...
MyObject& operator() (int coll, int row);
...
private:
}
Now, this allows me writing code like this:
MyMatrix mat;
MyObject obj1, obj2;
mat (2,3) = obj1;
obj2 = mat (3,2);
So far, so good.
MyMatrix also keeps some private constants that hold coordinates of some
special MyObjects, so retrieving of those special MyObjects is faster. For
example:
struct coordinates {
int col, row;
}
class MyMatrix {
public:
...
MyObject& operator() (int coll, int row);
...
private:
coordinates specialMyObject;
}
Because of operator(), client of MyMatrix is allowed to freely edit
contents of MyMatrix object, thus invalidating specialMyObject.
Now, I could write publicly available method Refresh () that refreshes
state of internal constants. There are two ways how this methods could be
used:
1.) Client must call it after editing MyMatrix object.
2.) It is called internally, by each method that relies on internal
constants, before those methods start their work.
Number 2.) is definitely bad because keeping state of object so it doesn't
have to be checked thoroughly each time needed, and then check it
thoroughly each time it is needed, completely nullifies purpose of internal
constants.
Number 1.) works fine, but from the OO view looks bad, because object
should take care of its internal data, not the client of that object. So,
there is something wrong with:
MyObject& MyMatrix:
perator() (int coll, int row);
I could either replace it with
MyObject MyMatrix:
perator() (int coll, int row);
and add method:
void MyMatrix::AsignObject (int coll, int row, const MyObject& obj);
but this invalidates expression:
mat (2,3) = obj1;
Or I could write some kind of proxy class that acts as a reference to
MyObject but also changes state of MyMatrix object when needed.
What would that proxy class look like? I don't know where to start from, so
links or google search keywords would be greatly appreciated.
I have a matrix class like this:
class MyObject;
// MyMatrix is contains MyObjects
class MyMatrix {
public:
...
MyObject& operator() (int coll, int row);
...
private:
}
Now, this allows me writing code like this:
MyMatrix mat;
MyObject obj1, obj2;
mat (2,3) = obj1;
obj2 = mat (3,2);
So far, so good.
MyMatrix also keeps some private constants that hold coordinates of some
special MyObjects, so retrieving of those special MyObjects is faster. For
example:
struct coordinates {
int col, row;
}
class MyMatrix {
public:
...
MyObject& operator() (int coll, int row);
...
private:
coordinates specialMyObject;
}
Because of operator(), client of MyMatrix is allowed to freely edit
contents of MyMatrix object, thus invalidating specialMyObject.
Now, I could write publicly available method Refresh () that refreshes
state of internal constants. There are two ways how this methods could be
used:
1.) Client must call it after editing MyMatrix object.
2.) It is called internally, by each method that relies on internal
constants, before those methods start their work.
Number 2.) is definitely bad because keeping state of object so it doesn't
have to be checked thoroughly each time needed, and then check it
thoroughly each time it is needed, completely nullifies purpose of internal
constants.
Number 1.) works fine, but from the OO view looks bad, because object
should take care of its internal data, not the client of that object. So,
there is something wrong with:
MyObject& MyMatrix:
I could either replace it with
MyObject MyMatrix:
and add method:
void MyMatrix::AsignObject (int coll, int row, const MyObject& obj);
but this invalidates expression:
mat (2,3) = obj1;
Or I could write some kind of proxy class that acts as a reference to
MyObject but also changes state of MyMatrix object when needed.
What would that proxy class look like? I don't know where to start from, so
links or google search keywords would be greatly appreciated.