你会如何分享两个其他对象之间的同一个对象?例如,我喜欢这种风格的东西:如何在两个类之间共享一个变量?
class A
{
private string foo_; // It could be any other class/struct too (Vector3, Matrix...)
public A (string shared)
{
this.foo_ = shared;
}
public void Bar()
{
this.foo_ = "changed";
}
}
...
// inside main
string str = "test";
A a = new A(str);
Console.WriteLine(str); // "test"
a.Bar();
Console.WriteLine(str); // I get "test" instead of "changed"... :(
在这里,我不想给予一个酒吧方法。我想实现的东西,看起来像在C++:
class A
{
int* i;
public:
A(int* val);
};
A::A (int* val)
{
this->i = val;
}
我看有一些REF /出的东西,但我不能让我问这里。我只能在我使用ref/out参数的方法范围中应用一些更改... 我也读过我们可以使用指针,但是没有其他方法可以使用它吗?
在你的例子中,字符串不是一个很好的例子,因为它们是不可变的。 – Ikaso 2010-03-29 04:49:01