Pointers
traditional pointers
int a = 1;
// Declaration
int* ptr = &a;
// print out the actual object inside the pointer
std::cout << *ptr << std::endl; // 1
// print out the address of the pointer
std::cout << ptr << std::endl; // 0x7ffd5268db54透過 Pointer 改變原本的物件
#include <iostream>
using namespace std;
int main() {
int a = 1;
int* ptr = &a;
std::cout << a << std::endl; // a is originally 1
// add the referenced object by 1
*ptr += 1;
// a is modified by ptr and becomes 2
std::cout << *ptr << std::endl;
std::cout << a << std::endl;
return 0;
}套用 Pointer 於 Functions 上
smart pointers
unique pointer
shared pointer
weak pointer
Last updated