On Thu, 13 May 2004 01:52:09 GMT, cppaddict <hello@hello.com> wrote:
[color=blue]
>Leor,
>
>Thank you very much. That is a nice solution. A couple questions....
>[color=green]
>>To control client operations, you can provide
>>the usual interface functions (overloaded const and con-const subscript
>>operators, for example),[/color]
>
>what are const and con-const subscript operators?[/color]
Well, if you're providing an interface that includes subscripting
operators, you generally need one that's a non-const member function (that
will be fine for applying to non-const objects), and another that's a const
member function (to allow const objects to be subscripted.).
Let's say you're implementing a class named MyVector, holding ints.. The
relevant portions would go something like this (untested code):
class MyVector {
public:
MyVector(...) {...}; // constructor(s)
int &operator[](size_t i) { return a_[i]; }
const int &operator[](size_t i) const { return a_[i]; }
...
private:
int *a_;
size_t size;
};
The idea is that if you have a const MyVector and you subscript it, the
result should be a reference to a const element, and if you have a
non-const MyVector, it should return a reference to a non-const. Const-ness
is important in C++ so that operations applied to function arguments that
have been declared as reference-to-const will be allowed (as long as
they're read-only):
void foo(const MyVector &mv)
{
cout << "The first value in mv is: " << mv[0] << endl;
}
The Above wouldn't compile if only the non-const operator[] were available,
because you can't invoke a non-const member function on a const object. If
you need to assign through the reference (using a non-const MyVector mv):
mv[0] = 10;
then it would use the non-const overload. I've tried to whittle this issue
down to the bare essentials, but it is rather involved ;-)
[color=blue]
>
>Also, I know you said using ref variables instead of pointer in main
>would be cleaner. I am curious why you did use pointers, because the
>code that follows works too.[/color]
But not with the same output, I'd wager. I'll explain down below.
[color=blue]
>
>Thanks again for your help,
>cpp[/color]
Glad to help.
[color=blue]
>
>PS: I had to change int i to unsigned int i in the loops b/c the
>compiler won't allow you to compare signed and unsigned ints (at least
>borland won't)
>
>
>----WORKING ALTERNATIVE------
>int main() {
> using namespace std;
> std::vector<int> vi = T::myStaticMemberVector();[/color]
Above, you're copying an entire vector upon return from the function. After
that, vi has no connection to the vector in the class. They're two separate
vectors.
[color=blue]
>
> for (unsigned int i = 0; i < vi.size(); i++)
> cout << "vector[" << i << "] = " << vi[i] << endl;
> cout << endl;[/color]
You wouldn't know that from the output (yet), because your vector vi is a
/copy/ of the one in the class!
[color=blue]
>
> vi.push_back(50);[/color]
The above changes your vector, not the one in the class.
[color=blue]
> vi = T::myStaticMemberVector(); // show no init this time[/color]
And you're now making yet another copy, and replacing the old vi (yours)
with a copy of the vector from the class.
[color=blue]
>
> for (unsigned int i = 0; i < vi.size(); i++)
> cout << "vector[" << i << "] = " << vi[i] << endl;[/color]
You should not be seeing the value 50 when you run it this way. If you
are, I need to go back to C++ school ;-)
Good luck,
-leor
[color=blue]
>
> return 0;
>}[/color]
--
Leor Zolman --- BD Software ---
www.bdsoft.com
On-Site Training in C/C++, Java, Perl and Unix
C++ users: download BD Software's free STL Error Message Decryptor at:
www.bdsoft.com/tools/stlfilt.html