C++_Reminders - RicoJia/notes GitHub Wiki

========================================================================

Cast

========================================================================

  1. dynamic_cast can be used to check if we get a derived. base -> derived ædep```cpp //In an array of ptr MyChild* = dynamic_cast<MyChild*> (ptr); //this will succeed if ptr points to a child class object, or it will return null if ptr points to a base class object. if(!ptr){...} //use !ptr here, I am not sure what type this is?

========================================================================
## Type and Templates
========================================================================
1. dependent typenames need typename at the front:
    - ```typename std::list<T>``` because there's a T in the list

2. Reminder: ```emplace_back``` doesn't work with ```std::initializer_list```
    - ```emplace_back``` is a templated function uses perfect forwarding. template deduction does not deduce ```{}``` into ```std::intializer```. But it works with ```std::initializer_list<TYPE>```
    - ```std::vector<double>``` works with ```std::initializer_list<double>``` because there's ctor ```std::vector<double>(std::initializer_list<double>)```
    - ```std::initializer_list<int>``` does not implicitly convert to ```std::initializer_list<double>```

3. Reminder: const member functions only work with ```const_iterator```
    ```cpp
    // if returning iterator, then see error: __normal_iterator<const int*,[...]>’ to ‘__normal_iterator<int*,[...]>
    std::vector<int>::const_iterator foo() const{
      return vec_.cbegin(); 
    }
    ```

========================================================================
## Pointers
========================================================================
1. ```unique_ptr.reset();``` destroys the object
2. Reminder: **Tricky Bug: accessing a freed memory: it might give you the same value**
    ```cpp
    ptr = node; 
    free(node); 
    cout<<ptr; 
    ```

⚠️ **GitHub.com Fallback** ⚠️