Quote:
|
I just need to brush up on exeception handling
|
The class C2DMatrixErroris a base class for specialized errors.
All I need to do is throw derrived classes from it as it fits my needs.
All I need to do is catch a C2DMatrixError& class. I've provided every class with an own what() member, derrived from the base class virtual what() member. so the base class object will always return the right version of what().
Caveat:
Obseverve the ampersand (&) in C2DMatrixError& closely (see main() function). Otherwise we won't enjoy polymorphism.
Tips:
- All what() versions for the derrived classes are the same, so you could have defined it in the base class only. However, you can specialize your what() members.
- This is better (add const): catch(const C2DMatrixError& err) {//...}
- This is better: void what() const;
- Observe how I overloaded operator()(). That is a new style. Doesn't mean I endorse it with all my heart.
- Observe how I overloaded TWO versions of operator()(). It is always a good idea that these overloads come in pairs: a const and a non-const version. Otherwise you won't be able to pass const types. Same goes when overloading operator[]() obvoiusly since this is the mother of our idea we have in this excercise.
- Remove the "cerr<<" from the class. A container shouldn't be responsible for output to anything. A container should contain
Quote:
|
IF I were to ever try to do what I was doing in my original post ( access that array via [ ] [ ]). How would I do it? Even though i wouldn't use that implementation anymore, it bugs me that I can't get the syntax for it to work. What's wrong with *start->array[1][2]=2; ?
|
From your original posted code, this works. See what happens.
Code:
object* obj = new object;
(*obj->array)[0][0]=12;
(*obj->array)[1][1]=24;
cout<<(*obj->array)[0][0]<<endl;
cout<<(*obj->array)[0][0]<<endl;
cout<<(*obj->array)<<endl;
Try it without encapsulation first to see it more clearly:
Code:
C2DVector<int> *array = new C2DVector<int>(5,5);
(*array)[0][0]=234;
cout<<(*array)[1][1]<<endl;
1b) Creating a STL container on the heap (using new) is hard to handle. Don't do that. Unless you need thousands of them.
2a) members: name_
2a2) std::string members: sName_
2a3) members as counter: nName_
2b) functions: some_function()
2c) classes: UppercaseName
Nothing fancy.