Posts

Showing posts from November, 2006

CTags and VIM

Sample generate tags file command ctags -f {tag file location} --recurse --langmap=C++:.C.h.c.cpp.hpp --languages=C++ --extra=+q --fields=+i --verbose {source parent folder e.g. /usr/my_source/} After tag file generated, add set tags= {tag file location 1}, {tag file location 2} to .vimrc in VIM type :set tags= {tag file location 1}, {tag file location 2} or add it to .vimrc in VIM For ultraedit, the ctag parameter is -L %fi -f %fo --language-force=C++ --extra=+q --fields=+i If using VIM as editor, the supported commands are :ts - tag, class definition as priority :tj - tag (can use /pattern or /^pattern. Since it is pattern search, it is not binary search mode which would be slow) :po or ctrl + 't' - older tag search stack ctrl ']' on selected text to call tag search directly. : or to scroll for previous calls (refer http://www.vim.org/htmldoc/tagsrch.html ) VIM commands which is useful when use in conjunction with ctags shift + * - start to search

Operator overload

A a; if(a) <--- this will trigger operator bool() and also operator void const*() const{ { } } boost style (typedef T * (this_type::*unspecified_bool_type)() const;operator unspecified_bool_type() const // never throws{return px == 0? 0: &this_type::get;}) if (a == "ss") <-- a combinations of few operator overload. Foo(std::string const& s); Foo(std::string s); bool operator==(Foo const& lhs, Foo const& rhs); For [] int& operator[] (unsigned i) { return data[i]; } class A { public: enum enA { A1, A2, A3 }; A() { } ~A() { } operator A::enA() { return A2; } }; void Fn(A::enA e) { std::cout << "Fn(A::enA)\n"; } when A a; Fn(a); // Will call Fn(A::enA) due to overload of operator A::enA()

C++ Heap Corruption example

class A { A(int i) { text = (char *)malloc(sizeof (char *)) ; } A() { text = (char *)malloc(sizeof (char *)) ; } virtual ~A() { if(text != 0) { free(text); text = 0; } } }; void main() { A a; a = A(123); }

Polymorphism or inheritance watch out

class B { char *str; B(){str = new char[100];} ~B(){delete [] str;} }; class D : public B { D(){} ~D(){} }; B* pB = new D;delete pB; // resource leak cause the base destructor (class B) wasn't call. Solution 1: The base class "MUST" declare destructor as virtual. e.g. virtual ~B(){delete [] str;} Solution 2: (a prevention mechanism to C++) By declaring class as final so that can't inherit the class. C++ itself does not have final, concept taken from Java. E.g. http://www.codeproject.com/cpp/finalclass.asp or use class __declspec(novtable) YourClassName

Excel VBA

A macro I used to creat the hyperlinks to another sheet which is in sequence Sub CreateLinks() Dim i, j, NoNeeded As Integer i = 19 'copy source start row j = 2 'copy destination start row NoNeeded = 500 'number of links Dim cellSelect As String Dim cellTarget As String For j = 2 To NoNeeded cellSelect = "A" & CStr(j) Range(cellSelect).Select cellTarget = "!A" & CStr(i) ActiveSheet.Hyperlinks.Add Anchor:=Selection, Address:="", SubAddress:=_ "'UNKNOWN (1)'" & cellTarget, TextToDisplay:="'UNKNOWN (1)'" & cellTarget i = i + 1 Next j End Sub