博文

[CPP] Exceptions and Lambdas

图片
How do we report errors? Return an error value from function set a global error code Throw an exception Exceptions Exceptions are “thrown” to report exceptional circumstances You can throw any type of object, fundamental type or pointer as an exception in C++ The standard class library provides some standard exception types, all derived from exception class You add handler code to catch the exception The stack frame is unwound, one function at a time (as if the functions returned immediately) until a catch which matches the type throw is found Catching exceptions First specify that you want to check for exceptions(try) Then call the code which may raise the exceptions Then specify which exceptions you will catch, and what to do The same as Java The catch clause A catch clause will match an exception of the specified type catch clauses are checked in the order in which they are encountered, the order of the catch clauses matters! Pointers and objects are ...

[CPP] Template classes and functors

图片
Template classes In the last blog, we introduce template functions: Also, we can make a template class, which is similar to generic class in Java. Here is a simple version of linked list to store integer value Let’s now using template to make it to store all kinds of data Add prior to each member function definition: template < typename T> Add the < T >to the end of the class name in the member function implementation/definition(if we have separate .h and .cpp file) Still remember we use it in STL containers : vector<int> vector1; Functors Simple functors functors are classes which overload () operator Functors with member data Functors taking variables by reference Functors vs Functions Here, what we did is to use a function pointer and apply function using this function pointer. But now i want to make the function pointer to functors or make it to use both , what should i do? Use Template! What we do here?! Be...

[ACE] Dynamic Programming

图片
Overview There are various general methods for finding solutions to problems. Common ones include: Brute force - “generate and test” Divide and conquer Heuristics Dynamic Programming Brute Force This is roughly “generate and test”, generate all potential solutions and test for which ones are actual solutions. This method is extremely inefficient, as is O(n!), but can be useful in some small cases. Divide and Conquer Recursively, break the problem into smaller pieces, solve them, and put them back together. Merge-sort and Quick-sort were classic examples of this. Heuristics Two common types: Decisions within a procedure that gives optimal answer to make it go faster. Decisions within a procedure that might not give optimal answers, but are designed to give good answers that are impractical to obtain otherwise. Examples: “Admissible heuristic” in A* search - decreases the search time compared to plain search “pick a random pivot” in quick sort Greedy algorith...

[CPP] Operator Overloading and templates

图片
We have covered basic operator overloading in the previous blog, to summarize it, operator overloading can: Can define/change meaning of an operator, you can make it as member functions you can use it by a-b, or converts it to a.operator-(b) or operator-(a,b) , Access rights like any other function, e.g. has to be a friend or member to access private/protected member data/functions, Also, parameter types can differ from each other, e.g. MyFlt operator-(const MyFlt &, int); would allow an int to be subtracted from a MyFlt. Continued-Assignment vs comparison For example, we have created one class. And we new two object of this class, then we want to compare if two object are the same. We can’t simply use ob1 == ob2 . Because there is no == operator defined by default. Pointers could be compared though, but not the objects themselves. That why we need == operator overloading. Questions to ask yourself Define as a member or as a global? If global then does it need ...

[CPP] Casting and operator overloading

图片
Casting Casting away the const-ness When we have a const value, normally we can’t change them, but using casting, we can cast the const value to remove the constness of a reference or pointer. WARNING: Do not actually do this unless there is a really good reason! Casting away const-ness is really bad. C++11 new way to cast Four new casts: cosnt_cast< newtype >(?) dynamic_cast < newtype >(?): safely cast a pointer or reference from base-class to sub-class static_cast< newtype >(?): cast between types, converting the type reinterpret_cast< newtype >(?) Four types give more control over what you mean, and help you to identify the effects. Sometimes needed : dynamic_cast provides run-time type checking . Note: Casting a pointer will not usually change the stored address value, only the type. This is NOT true with multiple inheritance. static_cast Commonly used cast, attempts to convert correctly between two types. The example is in the pr...

[CPP]Default members and conversion

图片
Default member functions Automatically generated functions 4 functions created by default if needed You can make them unavailable(e.g. private) Or change their behaviour A default constructor (no parameters needed) A copy constructor(copy one object to another) An assignment operator ( = operator) A destructor A default constructor Automatically created if and only if you do not create any other constructors This is why you can still create objects even when classes appear to have no constructors If you create any constructor, compiler will not create a default one for you. Since C++ 11 you can tell it to create a default one anyway by putting “= default” instead of “{}” ClassName() = default; if we comment on “DemoClass() = default;” This program will not compile. The copy constructor The copy constructor is used to initialize one object from another of the same type This includes when a copy is implicitly made: Passing object as a parameter i...

[CPP] Function pointers and virtual functions

图片
Function Pointers Functions are stored in memory, in code segament. You can ask for the address of them. You can store these in function pointers. void (*g1)() = NULL; The above code means g1 is a function pointer, the function takes no parameter and it returns void. Note that (*g1)(); and g1() both call f1() Here we change again , we assign g1 to g2: Remember g2 is just a pointer , g1 assign to g2 so g2 just assign the address of g1 and g1 points to f1, so when we call g2, what we actually did is to call f1(); Callback Function A callback is any executable code that is passed as an argument to other code, which is expected to call back (execute) the argument at a given time Briefly speaking, callback function is a function pointer(a address), you know this pointer or address, you can use it whenever and wherever you want. Therefore, you can use it as function argument. v-tables non-virtual For normal/default (non-virtual) functions: Type of pointer determ...