Topic: More about const and vector
Author: James Kuyper <kuyper@wizard.net>
Date: 1999/03/09 Raw View
Ahn Ki-yung wrote:
...
> Is this possible then ?
>
> vector<int> a;
>
> // ... ...
>
> const vector<int> copy_a = a;
Sure.
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: "Nate Lewis" <nlewis@mindspring.com>
Date: 1999/03/09 Raw View
Ahn Ki-yung wrote in message <36E48A80.B00757A9@daidun.kaist.ac.kr>...
>> This is conforming behavior. One of the requirements for using any
of
>> the standard containers (from 23.1/3 in the standard) is:
>>
>> "The type of objects stored in these components must meet the
>> requirements of CopyConstructible types, and the additional
requirements
>> of Assignable types."
>>
>> CopyConstructible is no problem, but const objects aren't Assignable,
so
>> you can't put them in any of the standard collections.
>>
>
>Is this possible then ?
>
>vector<int> a;
>
>// ... ...
>
>const vector<int> copy_a = a;
Sure; vector has a copy constructor that performs a deep copy.
Potentially expensive, of course. If the intent is to pass this vector
to other functions without allowing them to modify it, it's just as easy
to simply pass a const reference to the [non-const] object.
--
Nate Lewis, MCSD
nlewis@mindspring.com
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: Ahn Ki-yung <kyagrd@chiak.kaist.ac.kr>
Date: 1999/03/09 Raw View
> This is conforming behavior. One of the requirements for using any of
> the standard containers (from 23.1/3 in the standard) is:
>
> "The type of objects stored in these components must meet the
> requirements of CopyConstructible types, and the additional requirements
> of Assignable types."
>
> CopyConstructible is no problem, but const objects aren't Assignable, so
> you can't put them in any of the standard collections.
>
Is this possible then ?
vector<int> a;
// ... ...
const vector<int> copy_a = a;
// ... ...
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html ]