2013-09-11 180 views
0

我認爲它是關於時間我學會了區別。我已經看到如何使用這些,但教程有點模糊。我對語法以及如何在很小程度上使用它們有非常基本的瞭解。指針總是顯得有點嚇人和混亂。我聽說,一旦你學會使用它們,它們是偉大的,我想要一些偉大的:)。那麼,我應該如何使用這些以及我應該在什麼時候使用它們?還有一件事,我正在使用C++。參考文獻與指針

謝謝!

+0

@MooingDuck我沒有意識到這一點。 – retsgorf297

+1

這是您在屏幕上顯示的類似問題中排名第一的打擊,同時也位於此頁面右側的邊欄上,以及其他七個重複問題。我明白想問一個問題,但請閱讀屏幕上的內容:) –

+0

請不要毆打我,因爲有可能重複。我13歲,我想我的研究不太好。這是我在這個網站的第二次嘗試。我真的很喜歡這個,所以請不要讓我在這裏問問題。 – retsgorf297

回答

1

即使我在這裏鳴叫鴨小樣本可能闡明同意:

int nValue = 5; 
/* 
* &rfValue is a reference type, and the & means reference to. 
* references must be given a value upon decleration. 
* (shortcut like behaviour) 
* it's better to use reference type when referencing a valriable, 
* since it always has to point to a valid object it can never 
* point to a null memory. 
*/ 
int &rfValue = nValue; 

/* This is wrong! reference must point to a value. it cannot be 
* null. 
*/ 
//int &rfOtherValue; /* wrong!*/ 

/* same effect as above. It's a const pointer, meaning pValue 
* cannot point to a different value after initialization. 
*/ 
int *const pValue = &nValue; //same as above 

rfValue++; //nValue and rfValue is 6 
nValue++; //nValue and rfValue is 7 
cout << rfValue << " & " << *pValue << " should be 7" << endl; 
+0

感謝您解決這個問題! – retsgorf297