2013-05-12 35 views
4

層是節點的一個鋸齒形陣列,並且其表示的Theta陣列,每個節點作爲源[]和目的地[]。鏈接2個變量的一個對象

問題是,爲什麼當我改變第四行的代碼,第五行仍然打印「0」後,我聯繫的對象?在圖層的填充

theta t = new theta(); 
layers[1][i].source[j] = t; 
layers[0][j].destination[i] = t; 
layers[0][j].destination[i].weight = 5; 
Console.WriteLine(layers[1][i].source[j].weight); 

struct theta 
{ 
    public double weight; 
    public theta(double _weight) { weight = _weight; } 
} 

class node 
{ 
    public theta[] source; 
    public theta[] destination; 
    public double activation; 
    public double delta; 

    public node() { } 
    public node(double x) { activation = x; } 
} 

樣品:

node n = new node(); 
n.destination = new theta[numberOfNodesPerHiddenLayer+1]; 
n.source = new theta[numberOfNodesPerHiddenLayer+1]; 
layers[i][j] = n; 
+0

你應該澄清'layers'陣列的填充方式。 – 2013-05-12 19:14:45

+0

請張貼整個外部/嵌套循環(如果適用)代碼片段,因爲不清楚索引i和j來自哪裏。 Rgds, – 2013-05-12 19:16:12

+0

將結構更改爲類,一切都將工作。隨着目前的使用,在那裏有結構是沒有意義的 – ashcliffe 2013-05-12 19:17:18

回答

4

這是因爲西塔是一個結構,而不是類。結構被隱式複製。當你在做:

theta t = new theta(); 
layers[1][i].source[j] = t; 
layers[0][j].destination[i] = t; 

你最終得到三份't'。一個原件,一個在索引1,我和一個在索引0,j。然後,將5分配給其中一個副本。所有其他人保持未修改。這就是結構與類的不同之處:它們是通過值複製來分配的,而不是通過引用。

+0

謝謝!您的解決方案完美運作我認爲'struct'只是另一個類,但有一些限制,比如沒有函數/過程來簡化。我應該多讀書! d: – Auromius 2013-05-12 19:22:47