Trolltech Home | Qt-interest Home | Recent Threads | All Threads | Author | Date
All threads index page 3

Qt-interest Archive, April 2007
Qvector delete and variable initialization


Message 1 in thread

Hi this is rather c++ question than Qt
so sorry for that, but what i dont understand is if
I have a variable foo declared without initializing it and later 
initalize it with some value say its Qvector and i push some elements.
then i delete foo;
Can i later access the declared variable foo? if i want to intitialize 
it with some other values?

--
 [ signature omitted ] 

Message 2 in thread

On 10.04.07 10:32:31, suleiman wrote:
> Hi this is rather c++ question than Qt
> so sorry for that, but what i dont understand is if
> I have a variable foo declared without initializing it and later initalize it 
> with some value say its Qvector and i push some elements.
> then i delete foo;
> Can i later access the declared variable foo? if i want to intitialize it with 
> some other values?

Sure. The variable is still there. You can always do foo = new QVector<Foo>();
However you need to be careful, to make sure foo was really deleted
before doing the 2nd new, else you will leak memory. To make sure, it is
common to do something like this:

delete foo;
foo = 0;

then you can later on detect if foo really was deleted by doing
if(!foo).

Andreas

-- 
 [ signature omitted ]