1. 程式人生 > >[C++] reference to pointer 指標的引用

[C++] reference to pointer 指標的引用

	char *str = "Hello";
	char *ptr = str;
	char *&rptr = str;
	char *world = "World"; 
//------------------------------
	rptr = world;
	std::cout << str << std::endl; // Output: World
	char *str = "Hello";
	char *ptr = str;
	char *&rptr = str;
	char *world = "World"; 
//--------------------------------
	ptr = world;
	std::cout << str << std::endl;  // Output: Hello

str 和 rptr 名稱不同,但是是相同的指標,修改 rptr,str 隨之改變。引用的指標非法,指標需要指向物件,引用不是物件。

Reference to pointer