Talk About Network

Google


Register and Login
Nick
Password
Register create new account Sign up is FREE and you can post replies, new topics, bookmark posts and more!
Recover lost password


Programming > C++ > Re: class with ...
Latest [ Topics | Posts ] Archive Post A New Topic Post a Reply
<< Topic < Post Post 4 of 4 Topic 45894 of 46738
Post > Topic >>

Re: class with two dimensional array as a member (I need help)

by "Alf P. Steinbach" <alfps@[EMAIL PROTECTED] > May 13, 2008 at 12:05 PM

* James Kanze:
> On May 13, 9:18 am, "Alf P. Steinbach" <al...@[EMAIL PROTECTED]
> wrote:
>> * Pawel_Iks:
> 
> Just a couple of nits (and the correction of a typo), but...
> 
>> <code>
>> class A
>> {
>> private:
>>      size_t            myN;
>>      std::vector<int>  myElements;
>>
>>      size_t indexFor( size_t i, size_t j ) const
>>      {
>>          return myN*i + j;
>>      }
> 
>> public:
>>      A( size_t n ): myN( n ), myElements( n*n ) {}
> 
>>      int at( size_t i, size_t j ) const
>>      {
>>          return myElements.at( indexFor( i + j ) );
> 
> You certainly meant "indexFor( i, j )"

Yes.


>.  As written, it won't
> compile (and wouldn't do the right thing if it did).
> 
> Also, I very much question the wisdom of using at() here.

That's the design of std::vector::at.

It may be unwise (or not!) that std::vector::at doesn't terminate the 
program instead of throwing, but what it does that is absolutely not 
unwise is to guarantee to /fail/ on an invalid index value, which 
guarantee operator[] doesn't offer  --  all operator[] has is UB.

Especially for a newbie it's therefore a good idea to use at(), and a 
bad idea to use operator[], unless one knows that one's standard 
library implementation guarantees failure also for operator[]  --  but 
in general one won't know that, and in particular, a newbie won't.


>  If
> there's an error in the indexing, you want an assertion failure,
> not an exception.  Which is what you'll get with any reasonable
> implementation of std::vector<>::operator[].  But of course,
> even that is only partially right, since something like 20, 0
> will pass even if myN is 10.  You really need to use assert in
> indexFor, i.e.:
> 
>     size_t indexFor( size_t i, size_t j ) const
>     {
>         assert( i < myN && j < myN ) ;
>         return myN*i + j;
>     }

Ideally, yes, agreed.

I congratulate myself on still being able to write off-the-cuff code 
that is so easily modified, so maintainable. ;-)


Cheers,

- Alf

-- 
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
 




 4 Posts in Topic:
class with two dimensional array as a member (I need help)
Pawel_Iks <pawel.labed  2008-05-12 23:49:30 
Re: class with two dimensional array as a member (I need help)
"Alf P. Steinbach&qu  2008-05-13 09:18:07 
Re: class with two dimensional array as a member (I need help)
James Kanze <james.kan  2008-05-13 02:50:18 
Re: class with two dimensional array as a member (I need help)
"Alf P. Steinbach&qu  2008-05-13 12:05:23 

Post A Reply:
  Go here to Signup

AddThis Feed Button


About - Advertising - Contact - Frequently Asked Questions - Privacy Policy - Terms of Use - Signup

Contact
tan12V112 Wed Jul 9 0:02:57 CDT 2008.