Multiple objects passed as reference to function

Essentially, the 'Sequence' below uses linked lists to store data. If 'result' refers to the same sequence as 'seq1' or 'seq2', I want 'result' to refer to a new sequence. This new sequence can be default constructed (no copy of 'seq1' or 'seq2' is required). I can't seem to do this correctly. Also, the prototype of the function cannot be altered.

void fun(const Sequence& seq1, const Sequence& seq2, Sequence& result)
        // Check for reference to same sequence. If they are the same,
        // create new sequence for 'result' to refer to
	if ((&seq1 == &result) || (&seq2 == &result))
                // How do I create a new Sequence that wont be destroyed?
                // All I need is for 'result' to now refer to an empty sequence.
                // The following method is incorrect.
		Sequence* tmpSeq = new Sequence;
		&result = *tmpSeq;
        .....more code....

Thanks for any help you can give.
Last edited on
You should either made result a pointer, or do an assigment: result = Sequence();
result = Sequence();

I tried this, but, for example, if 'result' refers to 'seq1' it just made seq1 and 'result' both have a default constructed 'Sequence'.
Last edited on
At line 11, you're trying to set the address of result. That can't be right. You want to be copying the contents of the new, empty sequence into result.

Is Sequence properly copyable? Have you written a copy constructor for it?

Where are you freeing the memory that you allocate at line 10?

What are you expecting the calling code to pass in as the third argument? An "empty" sequence (presumably, a freshly-created one)? An existing sequence containing valid data?
Last edited on
I add: references cannot be redirected. They will point to the same memory area which was set when they were created. You should use pointers in your case.

Last edited on
Topic archived. No new replies allowed.