Wednesday, 15 June 2011

c++ - Need a heap allocated pointer, but I'm given a stack allocated pointer -


Somewhere in the

then I've got a class that includes a std :: unique_ptr like this:.

  class objectA {public: object A (); ~ ObjectA (); Some functions (); Private: std :: unique_ptr & lt; ObjectB & gt; MyPointer; }   

Then the source file in the file in myPointer is used in the manufacturer and used in SomeFunction () .

  Object A: Object A () {Object Object C; MyPointer = std :: move (std :: unique_ptr  (objectC.getPointer ())); // Setup pointer} ObjectA :: ~ ObjectA () {} Zero ObjectA :: SomeFunction () {// use myPointer here}   

However the problem is that I can not use myFoinction in myFoinction () , and why here.

Obviously myPointer should be allocated on a heap to make sure it is not that the constructor will be destroyed if executed. Assume that ObjectC and the resultant functions are from external libraries when I call ObjectC :: getPointer () , then pointer to be allocated on a heap of piles Is allocated. Now I think this is the case because after getting the correct execution of the producer, I get an error has ended.

Actually I'm counting on a function with which to give me an indicator I can use somewhere else. However, the function allocates on the stack rather than the pile.

Is there a special way to solve this problem, perhaps with a double pointer? Or do I just have to call the ObjectC :: getPointer () , every time I want to use the pointer inside each performance block? If I have many functions inside ObjectA , which depends on myPointer, then ObjectC :: getPointer () per function is useless, but I Don does not know that there is a better way to fix it, and I feel like a function ( ObjectC :: getPointer () ) that I should not force that redundancy.

When you call ObjectC :: getPointer () You just do not get the "a" indicator function should specify how well the operation pointer is, and how specifically it should be disposed of.

Usually, this will be delete , but this can also be an example fclose . You have to read the documentation. If the returned indicator matches the life of objectC , then the life of objectC must match the myPointer . So it should probably be a member, and in turn it means that myPointer can be unnecessary just you private option: objectb & amp; GetB () {Return * myObjectC.GetPointer (); }

No comments:

Post a Comment