搜尋此網誌

2025年5月30日星期五

Operator Precedence

explicitly: clearly or directly, so that the meaning is easy to understand

The unary plus operator does not change the value of the operand. It is primarily used explicitly indicate that a number is positive.

The unary minus operator negates the value of the operand, effectively multiplying it by -1.


int x = 5, y;

y = ++x; // x increases to 6, then y gets 6

std::cout << "Pre-increment: x = " << x << ", y = " << y << std::endl;

x = 5; // Reset x

y = x++; // y gets 5, then x increases to 6

std::cout << "Post-increment: x = " << x << ", y = " << y << std::endl;


Dot operator is used when accessing members of an object directly.

Arrow operator is used when accessing members through a pointer to an object.


When a function does not return any value, it is declared with void.


The subscript operator is used to access elements in arrays, vectors, or other indexed data structures. It allows direct indexing and retrieval of values stored in a sequential collection.

Direct indexing refers to accessing【電腦】存取(資料)elements in an indexed data by using their exact position, or index, without needing to search for them.


#include <iostream>

int main() {

    bool isRaining = false;

    if (!isRaining) {  // Negates 'false', making it 'true'

        std::cout << "Enjoy the sunshine!" << std::endl;

    }

    return 0;

}


#include <iostream>

int main() {

    int num = 42;

    int* a = &num;  // Pointer 'a' stores the address of 'num'

    std::cout << "Value of num: " << *a << std::endl;  // Dereferencing 'a'

    return 0;

}


In C++, the difference between a and *a depends on how a is defined, especially if it is a pointer.


In C++, sizeof is an operator that returns the size (in bytes) of a data type or object. It helps determine memory usage for variables, structures, arrays, and pointers.


Dynamic memory allocation in C++ allows you to allocate memory at runtime, instead of compile-time. This is useful when you do not know the exact amount of memory your program needs in advance.

Compile time occurs before execution and runtime occurs during execution.


A pointer-to-member is a special type of pointer that holds the memory address of a class member function or a variable, rather than an instance of the class itself.

#include <iostream>

class MyClass {

public:

    int value = 42;

};

int main() {

    MyClass obj;

    int MyClass::*ptr = &MyClass::value;  // Pointer to member variable

    std::cout << "Value using pointer-to-member: " << obj.*ptr << std::endl;  // Access using object

    return 0;

}


int num = 5;

int result = num <<2;

Here, 5 << 2 shifts the bits two places left:

00000101 (5) → 00010100 (20)


The ternary conditional operator in C++ is a shorthand for an if-else statement.

#include <iostream>

int main() {

    int a = 10, b = 20;

    int max = (a > b) ? a : b;  // If 'a' is greater, assign 'a', otherwise 'b'

    std::cout << "Maximum value: " << max << std::endl;

    return 0;

}


>>= equivalents to a = a >> b


The comma operator is used to evaluate multiple expressions in sequence, but it only returns the result of the last expression.

#include <iostream>

int main() {

    int a = (10, 20, 30); // Only '30' is assigned to 'a'

    std::cout << "Value of a: " << a << std::endl;

    return 0;

}


+= equivalents to a = a + b


pitfall: a danger or difficulty, especially one that is hidden or not obvious at first


Microsoft Copilot

www.oxfordlearnersdictionaries.com

沒有留言:

發佈留言