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):
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 ); }
};
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):
c l a s s v e c t o r _ b a s e {t e m p l a t e said:// clumsy use of constructor
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 ); }
};