Method passes values to a vector, but the vector stays empty?

Hi,

I have two classes, one class requests another class to populate a vector with some data using getAvailableStreams() method:

1
2
3
4
  vector<AutoRefPtr<ManifestStream> > availableStreams;
  std::cout << "now asking for streams." << std::endl;
  manifest->getAvailableStreams(&availableStreams);
  std::cout << "got so many streams: " << availableStreams.size() << std::endl;



The other class passes another pre-populated vector back:

1
2
3
4
5
6
7
8
9
10
    virtual RESULT getAvailableStreams(std::vector< AutoRefPtr<ManifestStream> >* availableStreams) {
        availableStreams = static_cast<std::vector< AutoRefPtr<ManifestStream> >* >(myStreams); //myStreams contains 1 stream

        // I'm also adding a stream here directly just for testing purposes:
        ManifestStream stream2;
        availableStreams->push_back(AutoRefPtr<ManifestStream>(&stream2));

        std::cout << "calling getAvailableStreams. Giving so many streams: " << myStreams->size() << ", " << availableStreams->size() << std::endl;
        return result_OK;
    }


If I run the code, I can see the following logs:

1
2
3
	now asking for streams.
	calling getAvailableStreams. Giving so many streams: 2, 2
	got so many streams: 0


Why am I getting 0 streams when I exist the method? I just had 2 streams in availableStreams while I was inside the method, where have they disappeared?

Will appreciate any suggestions!
Last edited on
When you assign to the pointer parameter it has no affect on the value being pointed to.

Unless you have a need for the parameter to be null, don't use pointers and instead pass by reference:
1
2
    virtual RESULT getAvailableStreams(std::vector< AutoRefPtr<ManifestStream> >& availableStreams) {
        availableStreams = myStreams;
I'm not sure what you needed the static_cast for, but I don't think you need it anymore.
Hi,

Thanks for your reply! I thought it was something to do with that... Unfortunately, I cannot change the signature of the function as this is a mock implementation of a function from another library, and the signatures have to be the same :(

Any other suggestions on how I can overcome this problem while not changing the function parameters? Surely there must be a trick to make this work? :)

The cast is needed because this vector actually contains mock version of streams.
Without changing signature:
1
2
    virtual RESULT getAvailableStreams(std::vector< AutoRefPtr<ManifestStream> >* availableStreams) {
        *availableStreams = myStreams;
monday to friday wrote:
The cast is needed because this vector actually contains mock version of streams.
You are violating certain rules and creating undefined behavior. If it works, it might not always work and it might depend on your compiler and compilation settings.
So there is no way to add values to a vector through a pointer that is passed to a function?
Didn't I just demonstrate that you could?
Last edited on
Oh sorry I misread your message. Thanks, I'll try this.
Topic archived. No new replies allowed.