作者ric2k1 (Ric)
看板EE_DSnP
標題Re: [問題] homework8
時間Sun Dec 23 22:06:04 2007
※ 引述《bumpwy (bumpwy)》之銘言:
: 想在問一個問題
: 作業中要我們在 readgraph的memberfunction中allocate the memory for all the Vertex object,
: but wouldn't these objects be invalid out of the function's scope?
Local pointer variables may be invalidated. However, the "pointed memory"
will reside unless you "explicitly" delete it.
You can do something like:
_toVertices = new Vertex*[_numToVertices];
Vertex* tmpPtr[_numToVertices];
for (size_t i = 0; i < _numToVertices; i++)
tmpPtr = new Vertex;
_root = tmpPtr[0];
...
..
.
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 59.121.133.136
1F:推 bumpwy:x 12/23 22:31