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++ > a question abou...
Latest [ Topics | Posts ] Archive Post A New Topic Post a Reply
<< Topic < Post Post 1 of 1 Topic 43728 of 48022
Post > Topic >>

a question about exception safety in the book 'the c++ programming

by unix.sh@[EMAIL PROTECTED] Mar 6, 2008 at 08:39 AM

Hi,

I am reading the book 'the c++ programming language' 3rd Edition by
Bjarne Stroustrup.

I have a question regarding the initialization. He said the v might be
0 in the following implementation(clumsy use of contructor). But I
think if v is not correctly initialized, an exception will be thrown
in this statement: v = alloc.allocated(n); So you won't get a vector
with v=0; It should have the same effect by saying
v(alloc.allocated(n)) in the constructor list. The following is the
code from the book:

vector implementation(clumsy use of contructor):

t e m p l a t e <c l a s s T , c l a s s A = a l l o c a t o r <T >
> // clumsy use of constructor
c l a s s v e c t o r _ b a s e {
p u b l i c :
A a l l o c ; // allocator
T * v ; // start of allocation
T * s p a c e ; // end of element sequence, start of space allocated
for possible expansion
T * l a s t ; // end of allocated space
v e c t o r _ b a s e (c o n s t A & a , t y p e n a m e A :: s i z e
_ t y p e n ) : a l l o c (a ), v (0 ), s p a c e (0 ), l a s t (0 )
{
v = a l l o c .a l l o c a t e (n );
s p a c e = l a s t = v +n ;
}
~v e c t o r _ b a s e () { i f (v ) a l l o c .d e a l l o c a t e
(v ,l a s t -v ); }
}

a right implmentation in E3.2:

t e m p l a t e <c l a s s T , c l a s s A = a l l o c a t o r <T > >
s t r u c t v e c t o r _ b a s e {
A a l l o c ; // allocator
T * v ; // start of allocation
T * s p a c e ; // end of element sequence, start of space allocated
for possible expansion
T * l a s t ; // end of allocated space
v e c t o r _ b a s e (c o n s t A & a , t y p e n a m e A :: s i z e
_ t y p e n )
: a l l o c (a ), v (a .a l l o c a t e (n )), s p a c e (v +n ), l a
s t (v +n ) { }
~v e c t o r _ b a s e () { a l l o c .d e a l l o c a t e (v ,l a s t
-v ); }
};
 




 1 Posts in Topic:
a question about exception safety in the book 'the c++ programmi
unix.sh@[EMAIL PROTECTED]  2008-03-06 08:39:10 

Post A Reply:
  Go here to Signup

AddThis Feed Button


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

Contact
tan12V112 Tue Oct 14 11:24:54 CDT 2008.