@related : Java function arg

c++


class Main
{
public:
  int member;
};

//#define SAFE

int main()
{

#ifdef SAFE
  deque<Main> v;
#else
  vector<Main> v;
#endif
  v.push_back( Main() );

#ifdef SAFE
  deque<Main>::reference r = v.back();
#else
  vector<Main>::reference r = v.back();
#endif

  cout << r.member << endl; // ok
  v.push_back( Main() );
  cout << r.member << endl; // invalid read
}

@tags: CPlusPlus

java

Java: FUNCTION ARGS ARE REFERENCES

But make sure that it can be changed : inmutable

public class Arg_copy 
{
  public static void f(String arg) { arg ="overide fail"; } // = is actually an allocator

public static void f(String arg[]) { arg[0] = “overide success”;}

  public static void  main(String[[]] s) {
String[] a= {"original"};
f(a[0]); 	System.out.println(a[0]); // original
f(a); 	        System.out.println(a[0]); // sucess
  }
}
reference.txt · Last modified: 2020/09/08 20:47 (external edit)
 
Except where otherwise noted, content on this wiki is licensed under the following license: CC Attribution-Share Alike 3.0 Unported
Recent changes RSS feed Donate Powered by PHP Valid XHTML 1.0 Valid CSS Driven by DokuWiki