PDA

View Full Version : Pass by reference



vermarajeev
20th July 2007, 11:03
How to force a class to pass by reference like how java does
(using C++)??

Thanks

jpn
20th July 2007, 11:19
This article (http://www.parashift.com/c++-faq-lite/references.html) should make it clear. Feel free to ask if still in doubt.

vermarajeev
20th July 2007, 11:44
This article (http://www.parashift.com/c++-faq-lite/references.html) should make it clear. Feel free to ask if still in doubt.

Hello jpn,
You didnt get me.

Let me explain...
-->in normal C++

class A
{
.....
};
void func( A &a ) //Is passed by reference
{

}
int main()
{
A a;
func( a );
return 0;
}

-->What I want???

class A
{
.....
};
void func( A a ) //should be forced to pass by reference always. This happens in java
{

}
int main()
{
A a;
func( a );
return 0;
}

jpn
20th July 2007, 11:46
As far as I know there is no other way around. "A &a" is how you do it in C++.

vermarajeev
20th July 2007, 11:49
As far as I know there is no other way around. "A &a" is how you do it in C++.

That is fine, but I need to make my class smart enough to provide such functionality. Is there some way I can implement my class like that???

Thanks

marcel
20th July 2007, 12:13
What do you mean?
You just pass the parameter by reference...

jacek
20th July 2007, 14:53
I need to make my class smart enough to provide such functionality.
This is counter intuitive in C++ and might lead to mistakes and confusion, but if you really want it, just implement data sharing class without copy-on-write mechanism (AFAIR it's called a single state class).