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

U

unix.sh

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 said:
// 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 ); }
};
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top