Simple pointer question (Sharing a vector between different classes)
I have a class AudioDeviceManager that passes a std::vector by reference to an AudioAnalyzer class.
I want other objects to be able to access this vector (that is passed to the AudioAnalyzer class). To do this, I created a pointer in the AudioAnalyzer class:
std::vector<float> *pointer;
I created a getter for it using Objective-C properties. When I am in the same class, I can seem to a开发者_JAVA百科ccess it fine and pass it to other objects (by reference)
NSLog(@"%i", [self pointer]);
det->process(time, *[self pointer]);
However, I can't seem to access it from another class, spectrum I initialize this spectrum class with the method call (fft_data is also a pointer to a std::vector)
- (id) initWithDataVec:(std::vector<float>*)address {
...
fft_data = address;
...
But NSLog(@"value: %i", fft_data);
returns 0.
Writing `NSLog(@"value: %i", &fft_data) returns what appears to be a valid address. But isn't this the address of the pointer?
I am not sure if my code is correct. I am looking for a way to "share" a single vector among different classes. Ideally, one class will receive a pointer to the vector that they can access at any time, without having to have a function called by another class.
You can't just pass (by reference) the same instance of the same std::vector to the constructors of all classes that need it?
精彩评论