Re: Passing Stream object by reference

From: Rolf Magnus (ramagnus_at_t-online.de)
Date: 05/08/04


Date: Sat, 08 May 2004 10:25:36 +0200

Ian Robertson wrote:

> I am trying to write a function that takes a reference to an object as
> its arguement. The object is created in another function and I am
> trying to pass the object to another function from within the function
> that created the object. I have cut out some code as I dont think
> showing a for loop is helpful.
>
> void TMP3_Main_Form::LoadMP3()
> {
> TMemoryStream *MP3Stream = new TMemoryStream();
> MP3Stream->LoadFromFile(FileListBox1->Items->Strings[i].c_str());
> GetMP3Data(&MP3Stream);
> ...
> ...
> ...
> }
>
> the object is created within this function. I load a file into the
> stream. I then want to pass that stream to another function to perform
> other tasks on the stream.
>
> My GetMP3Data function accepts a pointer to a stream as its arguement.

But above, a pointer to a pointer to a TMemoryStream is passed to
GetMP3Data. MP3Stream is a pointer to TMemoryStream, and &MP3Stream is
the address of that pointer. You want to pass the pointer itself, so
leave out the &.

> void TMP3_Main_Form::GetMP3Data(TMemoryStream *MP3)
> {
> char DATA[3]={0,0,0};
> MP3.Position = MP3.Seek(-128,soFromEnd);
> MP3.Read(DATA,3);
>
> /* Have tried
> MP3->Position = MP3->Seek(-128,soFromEnd);
> MP3->Read(DATA,3);
> also but this didnt seem to work either
> */

The latter is correct. Are you sure you got the error _within_ that
function? Unfortunately, you forgot to post the error messages you got.

> ...
> ...
> ...
> }
>
> I am having trouble with the * and & operators. I can use and process
> MP3Stream within the LoadTag() function but not with the GetMP3Data
> function. I am trying to avoid having to reload the file from disk
> from scratch in the GetMP3Data function to get the data into the
> function. Ideally I want to be able to load my file once into a stream
> in memory, do my processing with a couple of different functions on
> the same stream, and then rewrite the file back to disk from my
> updated stream. At the moment I have to keep reloading the file from
> disk as I cant pass by reference properly.
>
> Can anyone help?

I wonder where the reference is that you have been talking about at the
beginning. I only see pointers, no references.



Relevant Pages

  • Re: "C vs java"
    ... line or flushing the stream. ... as the Java side includes excess newlines. ... It seems to me that a reference /is/ a pointer. ...
    (comp.lang.c)
  • Re: open/fopen read/fread in multithreaded environment.
    ... variable,<dynamic memory are referred by differnt pointer of each ... The file pointers are local to the open stream. ... of pthread_mutex_lock indicates that it's at least a POSIX wannabe. ... if Multiple Threads trying to Read on Same File Descriptor <or Same ...
    (comp.programming.threads)
  • Re: fopen modes
    ... "The fopen function returns a pointer to the object controlling the stream." ... Kibblezy deen voo nizee! ...
    (comp.lang.c)
  • Passing Stream object by reference
    ... I am trying to write a function that takes a reference to an object as its ... I load a file into the stream. ... My GetMP3Data function accepts a pointer to a stream as its arguement. ... I am trying to avoid having to reload the file from disk from ...
    (comp.lang.cpp)
  • Re: Perform output if stream is good
    ... >> real stream, otherwise where did you get a reference to it. ... a boolean telling it if to use the stream or not. ... you can pass a null pointer when you don't want the ...
    (comp.lang.cpp)

Loading