peter koch wrote:
> qazmlp1...@rediffmail.com skrev:
>> Will the following program cause any problem, because of the missing
>> explicit copy constructor for the 'test' class?
>
> Yes. You will have double deletes.
No, there are no double deletes in the posted code.
> Depending on your real needs, you
> should use std::vector or std::string instead of the lowlevel pointer.
> In fact, operator new[] should almost never be used. Prefer std::vector
> (or in rare situations raw operator new)
>
> /Peter
>> #include <vector>
>> #include <iostream>
>>
>> class test
>> {
>> char* cPtr ;
>>
>> public:
>>
>> test()
>> {
>> cPtr = new char[10] ;
>> }
>>
>> ~test()
>> {
>> delete cPtr ;
>> }
>> } ;
>>
>>
>> int main()
>> {
>> std::vector< test*> vecTest ;
>>
Object allocated here, never copied:
>> test* testPtr = new test() ;
>>
>> vecTest.push_back( testPtr ) ;
>>
Object deleted here, exactly once.
>> delete testPtr ;
>>
>> vecTest.clear() ;
>> }
>
--
-- Pete
Roundhouse Consulting, Ltd. (
www.versatilecoding.com)
Author of "The Standard C++ Library Extensions: a Tutorial and
Reference." (
www.petebecker.com/tr1book)