Java與C++的函式參數傳遞比較
Java參數傳遞測試
Java的參數傳遞對於基本型態是複製,而物件型態則是類似C/C++的指標行為, 是複製物件的位址到函式中,所以在函式內assgin的新物件不影響外部變數所指的變數。
C++參數傳遞測試
而C++的參考則是會讓外部變數跟內部變數的狀態保持一致。
Java測試用程式碼
public class test {
static class MyObject {
public int iv = 0;
public String is = "initial field";
public MyObject io = this;
public MyObject() {
}
public MyObject(int primitive, String string, MyObject object) {
iv = primitive;
is = string;
io = object;
}
}
static public void setSomething(int primitive, String string, MyObject object) {
System.out.println("setSomething");
primitive = 200;
string = "do something";
object = new MyObject();
}
static public void setSomething2(int primitive, String string, MyObject object) {
System.out.println("setSomething2");
primitive = 300;
string = "do something 2";
object.iv = 400;
object.is = "set field";
object.io = new MyObject();
}
public static void main(String[] args) {
int pri = 100;
String str = "initial value";
MyObject obj = new MyObject();
System.out.println("int pri = " + pri);
System.out.println("String str = " + str + ", hash-code = " + Integer.toHexString(str.hashCode()));
System.out.println("MyObect obj = " + obj + ", hash-code = " + Integer.toHexString(obj.hashCode()));
System.out.println(" obj.iv = " + obj.iv);
System.out.println(" obj.is = " + obj.is + ", hash-code = " + Integer.toHexString(obj.is.hashCode()));
System.out.println(" obj.io = " + obj.io + ", hash-code = " + Integer.toHexString(obj.io.hashCode()));
System.out.println();
setSomething(pri, str, obj);
System.out.println();
System.out.println("int pri = " + pri);
System.out.println("String str = " + str + ", hash-code = " + Integer.toHexString(str.hashCode()));
System.out.println("MyObect obj = " + obj + ", hash-code = " + Integer.toHexString(obj.hashCode()));
System.out.println(" obj.iv = " + obj.iv);
System.out.println(" obj.is = " + obj.is + ", hash-code = " + Integer.toHexString(obj.is.hashCode()));
System.out.println(" obj.io = " + obj.io + ", hash-code = " + Integer.toHexString(obj.io.hashCode()));
System.out.println();
setSomething2(pri, str, obj);
System.out.println();
System.out.println("int pri = " + pri);
System.out.println("String str = " + str + ", hash-code = " + Integer.toHexString(str.hashCode()));
System.out.println("MyObect obj = " + obj + ", hash-code = " + Integer.toHexString(obj.hashCode()));
System.out.println(" obj.iv = " + obj.iv);
System.out.println(" obj.is = " + obj.is + ", hash-code = " + Integer.toHexString(obj.is.hashCode()));
System.out.println(" obj.io = " + obj.io + ", hash-code = " + Integer.toHexString(obj.io.hashCode()));
}
}
C++測試用程式碼
#include
#include
using namespace std;
class MyObject {
public:
int iv;
string is;
MyObject *io;
MyObject() :
iv(0), is("initial"), io(this)
{
// empty
}
MyObject(int primitive, string &string, MyObject &object) :
iv(primitive), is(string), io(&object)
{
// empty
}
MyObject(const MyObject &obj)
{
cout << "MyObject copy constructor" << endl;
iv = obj.iv;
is = obj.is;
iv = obj.iv;
}
};
ostream & operator <<(ostream &out, MyObject& obj) {
out << "MyObject@" << (void*) &obj;
return out;
}
void setSomething(int primitive, string &string, MyObject &object) {
cout << "setSomething" << endl;
primitive = 200;
string = "do something";
object = MyObject();
}
void setSomething2(int primitive, string &string, MyObject &object) {
cout << "setSomething2" << endl;
primitive = 300;
string = "do something 2";
object.iv = 400;
object.is = "set field";
object.io = new MyObject();
}
int main(int argc, char** argv) {
int pri = 100;
string str = "initial value";
MyObject obj = MyObject();
cout << "int pri = " << pri << endl;
cout << "string str = " << str << ", addr = " << (void*)&str << endl;
cout << "MyObect obj = " << obj << ", addr = " << (void*)&obj << endl;
cout << " obj.iv = " << obj.iv << endl;
cout << " obj.is = " << obj.is << ", addr = " << (void*)&obj.is << endl;
cout << " obj.io = " << *(obj.io) << ", addr = " << (void*)&obj.io << endl;
cout << endl;
setSomething(pri, str, obj);
cout << endl;
cout << "int pri = " << pri << endl;
cout << "string str = " << str << ", addr = " << (void*)&str << endl;
cout << "MyObect obj = " << obj << ", addr = " << (void*)&obj << endl;
cout << " obj.iv = " << obj.iv << endl;
cout << " obj.is = " << obj.is << ", addr = " << (void*)&obj.is << endl;
cout << " obj.io = " << *(obj.io) << ", addr = " << (void*)&obj.io << endl;
cout << endl;
setSomething2(pri, str, obj);
cout << endl;
cout << "int pri = " << pri << endl;
cout << "string str = " << str << ", addr = " << (void*)&str << endl;
cout << "MyObect obj = " << obj << ", addr = " << (void*)&obj << endl;
cout << " obj.iv = " << obj.iv << endl;
cout << " obj.is = " << obj.is << ", addr = " << (void*)&obj.is << endl;
cout << " obj.io = " << *(obj.io) << ", addr = " << (void*)&obj.io << endl;
return 0;
}
留言