Passing arguments by references

Started by
7 comments, last by Decrius 13 years, 4 months ago
Let's pretend that there exists a compiler that accepts multiple indirections of an object, and that it's possible in C++ (By all means, just pretend. :D)

I have this function:
void function (int & a){   std::cout << *(&(*(&(*(&a)))));   return;}


Do you think that this "&" symbol can negate "*" symbol? What are the differences between these two?

Now, we're back to real-life C++:
void function (int & a){    std::cout <<  [SOMETHING];}

What should I put in [SOMETHING] that makes this function correct and is able to print out the int a?

Thanks in advance.
Advertisement
Quote:
Let's pretend that there exists a compiler that accepts multiple indirections of an object, and that it's possible in C++ (By all means, just pretend. :D)

This is allowed.

Quote:
Do you think that this "&" symbol can negate "*" symbol? What are the differences between these two?

This sounds like homework. Do you know what happens when you use the unary & operator? What about the unary * operator? What are the types of the resulting expressions?

Quote:
What should I put in [SOMETHING] that makes this function correct and is able to print out the int a?

There is no special syntax required for getting the value of the reference - the reference can be considered an alias for the original value.
No, it's not homework, although it sounds like one. This is something I'm trying to tackle all by myself.

& : It gives the address of the value of something.
* : It gives the value of the address of something.

The results give an error. Error of In-direction.

However, it wouldn't let me do "pass by reference from an argument, and get the value of the passed reference's value the reference address is referencing."

Wordy...

So, for std::cout, I could get what the passed argument's value is, right?

It wouldn't work for function arguments that way...

int GCF (int a, int b){   int gcf = 0;   while (true)   {      gcf = a % b;      if (gcf == 0)         return b;      a = b;      b = gcf;   } }int foo (int & a, int & b){   return GCF (a, b);   //ERROR}
There's nothing wrong with the codes you have posted. The chaining of &/* pairs is valid, to output the value the reference refer to you just output the reference itself (std::cout << a), and there's no error with the call to GCF.

What problems are you having? If the codes you posted produces errors for you, you probably have a broken compiler or you're not telling us the true story.
That above is the true story, I'm not hiding anything. Could be a faulty compiler. I knew I should've not uninstall VS2008 and used VS2010.

Thanks for the troubleshooting.
So what exact errors are you getting in, say, the last example you posted? Copy and past the error message to not miss any detail. If I copy and past the code as it is into an empty file and compile, I get no errors at all.
Are you compiling the code as C++? References are not available in C, and the compiler might be confused by the syntax if you try to use them.
Quote:Original post by rip-off
Are you compiling the code as C++? References are not available in C, and the compiler might be confused by the syntax if you try to use them.


Yeah, that's the problem...Guess it's not a faulty compiler after all. :D

Well, now that I've found th real culprit, there's no need to ask more about this (solved) problem. Anyway, thank you everyone. :)

Unless, of course, the object at hand has non-trivially implemented * and & operators.
[size="2"]SignatureShuffle: [size="2"]Random signature images on fora

This topic is closed to new replies.

Advertisement