Re: Copying void *?
Travis <travis.bowers@gmail.com> wrote:
> So I had a Tree (custom written) that had, as a node type, as pretty
> simple class. The class just stored some std::string's plus a void*.
>
> So my tree instantiation use to be Tree <myClass *> myTree so adding a
> new looked something like
>
> myTree.Insert( new myClass( "string 1", "string 2", (void*) 2) ); //
> seemed to work fine
>
> Then I decided it was silly to store a tree of pionters, and changed
> the tree to Tree <myClass> myTree so adding looked more like...
>
> myTree.Insert( myClass( "string 1", "string 2", (void*) 2) ); //
> crashes?
>
> This insertion style compiles but the later one it crashes and I'm not
> sure why. It would *appear* that the void* at some point goes NULL or
> otherwise erroneous.
>
> Just looking for some educated thoughts/opinions. Thanks.
The thread seems to be focusing on the fact that you are casting a
number to a void*, but it seems to me that your question involves why
Insert( new MyObj() ) works but Insert( MyObj() ) doesn't.
I expect it crashes because, in the second case the object that was
inserted ceases to exist as soon as the Insert function returns.
|