Learning to be Giant.

C++ Learning Notes I

|

Recently I'm learning C++ again. After two years of non-cpp life, I have almost forgot everything about this language. This time, since a new edition of C++ Primer which covers the C++11 standard came out, I decided to pick it up. I will write down while learning some gists about the new standard and some points that I misunderstood before for future reference.

Typecast Overloading

I came across this code http://www.cppblog.com/airtrack/archive/2012/09/16/190828.aspx:

const class nullptr_t
{
public:
	template<class T>
    inline operator T*() const
        { return 0; }
        
    template<class C, class T>
    inline operator T C::*() const
        { return 0; }
 
private:
    void operator&() const;
} nullptr = {};

These codes are used to define nullptr before c++11. I was confused about operator T*(). But it turns out to be an overload of typecast. Just like operator int().

BTW, nullptr in new standard means a null pointer. It can implicitly convert to any type of pointers. Similar to NULL and 0.

const char *, char * const, char const *

This is a quite tricky problem often encountered at interview. The easy way to resolve this is to read the whole thing from right to left, with * spoken as “pointer to”. Therefore, the these three become:

  • const char *: pointer to const char, which means the char pointed to is const
  • char * const: const pointer to char, which means the pointer is const
  • char const *: this is the same with the first one.

Therefore, we could arrive at the conclusion that when const is located before a *, it modifies the type. Otherwise, the pointer.

constexpr

A constant expression is an expression whose value cannot change and that can be evaluated at compile time. - C++ Primer, 5th edition

constexpr is used to help enable compilers to help programmers to check if the expression is const and then can be evaluated in the compilation process.

const int max_files = 20; // max_files is a constant expression 
const int limit = max_files + 1; // limit is a constant expression 
int staff_size = 27; // staff_size is not a constant expression 
const int sz = get_size(); // sz is not a constant expression

constexpr int sz = size(); // ok only if size is a constexpr function

constexpr function is introduced in the new standard, and can be used to initialize a constexpr variable.

auto

auto enables programmers to not specify variable type explictly. Compiler can infer the type of variable from the initialization.

auto i = 0, *p = &i; // ok:i is int and p is a pointer to int 
auto sz = 0, pi = 3.14; // error:inconsistent types for sz and pi

From the above code we can see that similar to normal variable definition, the two variables defined in the same expression should be of the same type.

In particular, when we use a reference as an initializer, the initializer is the corresponding object. - C++ Primer, 5th edition

int i = 0, &r = i;
auto a = r; // a is an int(r is an alias for i, which has type int)

Meanwhile, auto will not be resolved to const types. In the C++ Primer, 5th edition, this is call top-level const.

top-level const indicates that an object itself is const.

and auto will ignore top-level const.

const int ci = i;
auto b = ci; // b is an int instead of const int

Misc.

  1. The signed values are automatically converted to unsigned.

     int a = -1;
     unsigned int b = 1;
     cout << a * b << endl; // This will output 4294967295
    
  2. typedef is different from #define

     typedef char *pstring;
     const pstring cstr_1 = 0; // This is a const pointer to char
     const char *cstr_2 = 0; // This is a pointer to const char
    

Reference

C++ Primer, 5th edition

Disclaimer: This is a personal weblog. The opinions expressed here represent my own and not those of any entity with which I have been, am now, or will be affiliated.

Comments