1. install Homebrew
@ http://brew.sh/
2. install GDB with Homebrew
$ brew tap homebrew/dupes
$ brew install gdb
3. codesign gdb
@http://ntraft.com/installing-gdb-on-os-x-mavericks/
Thursday, June 18, 2015
Saturday, June 13, 2015
Uniform Iterative&Recursive solutions for tree traversal in in/pre/post order
Friday, June 12, 2015
Calling another ctor in one ctor
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 | #include <iostream> using namespace std; class Widget { public: Widget(int n) { data = n; cout <<"Widget(int n): this=" <<this <<endl; } Widget() { cout<<"Widget(): this=" <<this <<endl; Widget(8888); } ~Widget() { cout<<"~Widget(): this=" <<this <<" data=" <<data <<endl; } void print() const { cout <<"print(): this=" <<this <<" data=:"<<data <<endl; } private: int data; }; int main() { Widget a; a.print(); return 0; } |
output:
Widget(): this=0x7fff556f9c08
Widget(int n): this=0x7fff556f9b90
~Widget(): this=0x7fff556f9b90 data=8888
print(): this=0x7fff556f9c08 data=:1794977846
~Widget(): this=0x7fff556f9c08 data=1794977846
Can I call a virtual function from ctor in C++?
Yes, but be careful. Actually, never call a virtual function in ctor or dtor.
@ the answer form the author of C++
@ the answer form the author of C++
@http://hilite.me/
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 | #include <iostream> using namespace std; class Shape { public: virtual void draw() { cout << "draw a Shape." << endl; } }; class Circle : public Shape { public: void draw() { cout << "draw a Circle." << endl; } }; class Base { public: Base() { cout << "Base()" <<endl; f(); Shape* shape = new Circle; shape->draw(); } virtual void f() { cout << "Base::f()" << endl; } }; class Derive : public Base { public: Derive() { cout << "Derive()" << endl; } void f() { cout << "Derive::f()" << endl; } }; int main() { Base* b = new Derive; return 0; } |
Base::f()
draw a Circle.
Derive()
Saturday, June 6, 2015
No virtual ctor in C++
We can't inherit constructors, so there is no need for them to be virtual.
A vtable is made for each class having one or more 'virtual functions'. Whenever an object is created of such class, it contains a 'virtual-pointer' which points to the base of corresponding vtable. Whenever there is a virtual function call, the vtable is used to resolve to the function address.
Ctor can not be virtual, because when ctor of a class is executed there is NO vtable in the memory, meaning no virtual pointer defined yet. Hence the ctor should always be non-virtual.
If you think logically about how ctor works and what the meaning/usage of a virtual function is in C++ then you will realize that a virtual ctor would be meaningless in C++. Declaring something virtual in C++ means that it can be overridden by a sub-class of the current class; however the ctor is called when the object is created, at that time you can NOT be creating a sub-class of the class that you must be creating, so there would never be any need to declare a ctor virtual.
@ the answer from the author of C++
A vtable is made for each class having one or more 'virtual functions'. Whenever an object is created of such class, it contains a 'virtual-pointer' which points to the base of corresponding vtable. Whenever there is a virtual function call, the vtable is used to resolve to the function address.
Ctor can not be virtual, because when ctor of a class is executed there is NO vtable in the memory, meaning no virtual pointer defined yet. Hence the ctor should always be non-virtual.
If you think logically about how ctor works and what the meaning/usage of a virtual function is in C++ then you will realize that a virtual ctor would be meaningless in C++. Declaring something virtual in C++ means that it can be overridden by a sub-class of the current class; however the ctor is called when the object is created, at that time you can NOT be creating a sub-class of the class that you must be creating, so there would never be any need to declare a ctor virtual.
@ the answer from the author of C++
const data members in C++
1. Constant member of an Object:
the constant member gets initialized in the initialization list:
class A
{
A(int size); //
const int SIZE ;
};
A::A(int size) : SIZE(size)
{
}
A a(100); // value of SIZE in object a is : 100
A b(200); // value of SIZE in object a is : 200
The whole idea for a constant is that you can initialize it but you cannot change it.
2. Const member of a Class:
2.1 using enum (int only)
class A
{
enum { SIZE1 = 100, SIZE2 = 200}; // enum const member
int array1[SIZE1];
int array2[SIZE2];
};
2.2 using static
class A
{
static const int a = 100;
static const pi = 3.1415926;
};
the constant member gets initialized in the initialization list:
class A
{
A(int size); //
const int SIZE ;
};
A::A(int size) : SIZE(size)
{
}
A a(100); // value of SIZE in object a is : 100
A b(200); // value of SIZE in object a is : 200
The whole idea for a constant is that you can initialize it but you cannot change it.
2. Const member of a Class:
2.1 using enum (int only)
class A
{
enum { SIZE1 = 100, SIZE2 = 200}; // enum const member
int array1[SIZE1];
int array2[SIZE2];
};
2.2 using static
class A
{
static const int a = 100;
static const pi = 3.1415926;
};
Friday, May 29, 2015
Principles of reusable Object-oriented design
1. Program to an interface, not an implementation
Don't declare variables to be instances of particular concrete classes. Instead, commit only to an interface defined by an abstract class.
2. Favor object composition over class inheritance
This keeps each class encapsulated and focused on one task.
Don't declare variables to be instances of particular concrete classes. Instead, commit only to an interface defined by an abstract class.
2. Favor object composition over class inheritance
This keeps each class encapsulated and focused on one task.
Wednesday, May 20, 2015
Binary Tree Tranversal
Inorder Traversal : LVR
Preorder Traversal: VLR
Postorder Traversal: LRV
depends on the position of V.
Level Traversal: BFS
Preorder Traversal: VLR
Postorder Traversal: LRV
depends on the position of V.
Level Traversal: BFS
Tuesday, January 20, 2015
Git cheat sheet
git clone
git branch hotfix@tianli
git checkout hotfix@tianli
modify code
git commit –a
arc diff
git checkout master
git pull
git checkout hotfix@tianli
git rebase master
git checkout master
git merge hotfix@tianli
git push
next round …
Monday, January 19, 2015
Phabricator + Git: a good practice for Scrum
Phabricator can be used to record all the backlogs for your sprint. The most amazing thing is to integrate with Git you can submit your code changes for review whenever you like.
Monday, January 5, 2015
Ignore the first line in a text file
Problem:
ignore the first line in a text file under Linux
Solution:
1) cat file |awk 'NR>1{print$0}'
2) cat file |sed 1d
Example:
$ cat file
1000 1000
8 8
2 -2
3 0
7 5
ignore the first line in a text file under Linux
Solution:
1) cat file |awk 'NR>1{print$0}'
2) cat file |sed 1d
Example:
$ cat file
1000 1000
8 8
2 -2
3 0
7 5
$ cat file |sed 1d
8 8
2 -2
3 0
7 5
2 -2
3 0
7 5
Subscribe to:
Posts (Atom)