in C++, TCPPPL

TCPPPL Chapt 7 : Pointers

As a former C programmer (as a student), this seems to be a familiar topic to me : really, the only thing you need to learn about C is pointers. However, people like me need to be aware of certain things.

void *. The book mentions that void * must be explicitly converted before it is assigned to a certain pointer type. I can’t remember that if you can use it without cast in C. Anyway, this is the reason why nullptr is better than NULL, according to the book. ( NULL may be macro of (void *)0 ). read more

TCPPPL Chpt6: ISO C++ Standards

Apparently this chapter is like a law book. I will left this as a list and I will finish this later when I finish this book.

  • Implement defined features, unspecific, and undefined
  • Hosted vs Freestanding implementation
  • Source character set
  • int/pointer to bool
  • char impl defined signed/unsigned : ok to convert to unsign ed
  • int{c} construct int from a char
  • 3digit octal
  • multicharacter literal
  • Unicode
  • int –
  • prefix and suffix, user defined literals
  • constexpr in <limits>
  • <cstdint> for int with variance needs
  • alignof
  • declaration and definationm,
  • scope: refer to global and self init
  • {} in declaration
  • missing initializers
  • () and function
  • auto
  • decltype
  • lvalue and object, and rvalue
  • objects lifetime
  • read more

    Basic of C++: A tour

    It is very interesting for a language book to have a quick tour before everything. Actually, it is a great idea for people whose first language is not C++. People just don’t have to go through the basic things like control flows to know a language.

    Generally, I have a feeling that C++’s code explain more itself. Programmers know more about what they are doing instead of checking documents for reference.

    The Basics

    Chapter 2 covers the very basic things (that would include control flows). First thing to note is that the introducing of namespace. Namespace in C++ seems to be clear and straightforward. What is not-so-straightforward is the name of header file/shared object library. Well, Java’s package infrastructure makes things more clear, though trying to put everything inside its structure probably is not so clever.

    I didn’t expect C++ provides things like ‘auto’. (Well, not the ‘auto’ when I first use C language a few years ago.) Anyway, C++ is not a dynamic tool language. However, if you think about it, it is nothing runtime here, and you just need a compile time check. To be a real dynamic language, the thing you need is a flexible variable type (which is implemented in runtime even if you could do it in compile time) and generic programming for functions.

    We finally have bools in C++!

    C already have const. It was a beautiful promise that one should keep. constexpr does more than that. It calculates the value at compile time. When using constexpr as decorator of a function, it will evaluate at compile time when possible. Those function must be as simple as only having a return statement. Note that if a value is const, the compiler will assume you to keep the promise. Conclusion: Do keep your promise or you will have something unexpected.

    Structs are improved. Say goodbye to the strange typedef!

    Operators are functions, which they should be! Just need to have it in mind that if the value is lvalue or rvalue, to make the return type reference when necessary.

    We also got a good syntax sugar for initiating fields. And a real enum class which is more than int.

    C++ provides a more standard way for error handling. Exception introduced just like all of the other modern programming languages. Static assertions provide error handling on compile time.

    There are some designing ideas. Invariant are restrictions applied to class definitions, and should be present in the code. If not, the functions should be independent rather than a member.

    Abstract mechanism

    As we can find in any articles about OOP, there is some mechanism in classes, like concrete/abstract class and class. There are some concerns under data storage since in C family you have to care more about memory, like the idea of the container. Also, note the following format for heap allocated arrays.

    double *elem = new double[s];
    delete[] elem;

    Instead of making all functions virtual, we need to explicitly specify if a function is a virtual one. A class with at least a virtual function is called abstract class, which can not be instantiated. The mechanism behind overriding is vtbl. Usually, this is to add a 4 byte for pointer at the beginning of each object. If we want to call the virtual function in the base class, we can use scope resolution (::) operator to specify the class name, and this is called suppressed.

    Constructor and destructor are called in a certain order. We don’t need to do anything for the constructor. However, for destructors, we need to make them virtual on base classes. Otherwise, only one of them is called.

    An important designing idea for C++ is cautious against pointers. In C code, it is not strange for a function to return a raw pointer which points to something in heap. However, it is no longer recommended in C++. Destructors help us manage heap memory more freely. Moreover, we have unique_ptr as a smart pointer.

    We need to note that “this” is a pointer, as it is in Java. Also, a const method is one that doesn’t change the object. Moreover, we can specify access-specifier when inheriting.

    Copy and move. The default behavior of C++ copy is shallow copy since it just does a copy bitwise. To make it copy deeply, we just need to override operator =. Moreover, C++ provides the std::move mechanism.

    std::move is a fast way to generate “right value reference”. In many cases, we don’t really need to copy the whole objects, especially when it is a container( such that copy is expensive. )  && is the mark for right value reference. When it is a parameter of a function, it (instead of the const &) can match the right value reference, to do a memory-economic behavior in the function.

    If we don’t want certain behavior in the derived class, we can use the “=delete” mechanism.

    class Shape{
        //no copy
        Shape(const Shape&)=delete;
        Shape& operator=(const Shape&)=delete;
        //no move
        Shape(Shape &&)=delete;
        Shape& operator=(Shape &&)=delete;

    Template. Generic programming is very useful for OOP. C++ has the template grammar:


    template<typename T>


    template<typename T, typename U>


    template<typename T, typename... Tail>
    void f(T head, Tail... tail){

    To override the operator(), we can use it as a functional object. For instance

    <typename T> class Less_than{ const T val; public: Less_than(const T &v): val(v){} bool operator()(const T &v) const {return v<val;} } read more

    Enough for Java. Let’s look at something new with C++

    Java is a good tool. It arranges everything for you and you don’t have to worry about the details. For those who do want to know about the details, it is interesting to dig a little bit into it. However, perhaps I don’t have time for it now.

    I tried Visual Basic when I was in middle school, but I didn’t really do anything nontrivial with it. Thanks to the university I attended for undergraduate courses, my first serious programming language was C. I am proud of this fact since in C you can handle many things in the hardware or OS level. You don’t have many shortcuts, which means you have to build it with your hand. That gave me much freedom to play with pointers to do things I could never imagine when I had to switch to Java when I was a Master student.

    I am happy to work with C++ now. C++ is not C with pointers, at least for Bjarne Stroustrup. I was surprised that he didn’t recommend pointer arithmetics and malloc for heap memories, which was very fun when I write C. After all, probably no one likes C++ in C styles. I will go check what C++ has for me. Heh.

    mockable-24-android.jar : Sometime too large

    As a TA of the course “Mobile Application App”, I recently run into a problem. The teacher asks students to upload their homework to LATTE, our university’s course management platform. However, the size limit of LATTE platform is 20 MB, and



    As Professor Chaturvadi suggests that we’d better stick on Latte for homework submission, I tried to load the apk. Actually, it does work on my AVD. But the thing is, although apk is actually zip, many files are turned into binary. Android Studio 2.2, which is released on 9/19, has a tool to decompile the code. However, I bet none of us want to read decompiled code like this:

    If we want to stick on LATTE, we can definitely ask students to remove $rootDir/build/mockable-android-24.jar. The conclusion comes from Android Studio’s document regarding their new build system: read more