java - Assignments are by reference? -
in java if do
myobj 1 = new myobj(); myobj 2 = one;
then 1 , 2 pointing same object.
in c++ if did:
myobj 1 = new myobj(); myobj *two = &one;
is same java example? wherein modifying either one
or two
effect both objects?
these create 1 , 2 same thing:
myobj *one = new myobj(); myobj *two = one; //one , 2 same thing, pointer's myobj myobj one; myobj *two = &one; //one object deallocated @ end of current scope //two pointer 1
in c++ new myobj()
creates myobj
on heap , returns pointer it.
myobj *one; //creates variable should point myobj instance myobj 2 = new myobj(); //doesn't work *two = &one; //get 2 point's @ , assign location of 1
update:
myobj 1 = new myobj(); //this doesn't quite work, assigning pointer class myobj *two = &one; //this works, create pointer point 1
to prepare first statement, use:
myobj 1 = myobj(); //myobj one(); //this requires constructor take arguments, juanchopanza! myobj one{}; //requires c++11, courtesy juanchopanza myobj one;
all same thing
generally, next should create copy:
myobj one; //or variant myobj 2 = one;
or if prefer utilize pointers:
myobj *one = new myobj(); myobj *two = new myobj(); *two = *one;
java c++ variable-assignment
No comments:
Post a Comment