搜尋此網誌

2025年7月8日星期二

Arrays, Pointers and Strings

*distances.begin()

Adding the asterisk * dereferences that iterator, giving you access to the actual value stored at that position.

When you declare something as public in a class, it means it’s accessible from outside the class.

class MyClass {
public:
    void sayHello() {
        std::cout << "Hello from public method!" << std::endl;
    }
};

int main() {
    MyClass obj;
    obj.sayHello();  // Totally fine—sayHello is public
    return 0;
}

The arrow operator (->) in C++ is used to access members (variables or functions) of an object through a pointer. It’s essentially shorthand for dereferencing a pointer and then accessing a member.

In C++, a C string (also called a C-style string) is a sequence of characters stored in a character array and terminated by a null character ('\0'). 

In C++, arrays and pointers are closely related --- but not identical. Their relationship is one of the most fundamental (and sometimes confusing) aspects of the language.

Array name as a pointer: In most contexts, the name of an array decays into a pointer to its first element.

int arr[5] = {10, 20, 30, 40, 50};
int* ptr = arr;  // Equivalent to: int* ptr = &arr[0];

Microsoft Copilot

沒有留言:

發佈留言