Wednesday, October 17, 2007

Switch between Windows debuggers

The key "Debugger" under registry:
\HKEY_LOCAL_MACHINE\Software\Microsoft\Windows NT\CurrentVersion\AeDebug
specifies which debugger will be invoked in case of application unhandled exceptions are thrown.

Usually, if Visual Studio is installed, the default debugger will be Visual Studio JIT Debugger("C:\WINDOWS\system32\vsjitdebugger.exe" -p %ld -e %ld); But if we want to use Dr. Watson(drwtsn32 -p %ld -e %ld -g) to debug (creating log and crash dump), we can set the key name of Dr. Watson to "Debugger" and VS JIT Debugger to something else.

Wednesday, September 12, 2007

About Copy Constructor

In the example below, push_back will call the copy constructor of Family implicitly. In this case, it's a deep copy. If we comment out the copy constructor, a default one will gets called, which is a shallow copy.

If we have at least one complex object in a class, we should think about writing our own copy constructor. (choose between deep copy and shallow copy) If there's no complex object in the class, a default copy constructor will do since there's no difference between deep and shallow copy in this case. Remember that if we provide our own copy constructor, we also need to write the destructor so the complex objects can be deallocated.

class Person
{
public:
int age;
Person()
{
age = 0;
}
};

class Family
{
public:
Person* houseHold;
int aptNum;
Family()
{
houseHold = 0;
aptNum = 0;
}
Family(const Family& family)
{
this->houseHold = new Person(*family.houseHold);
this->aptNum = family.aptNum;
}
};

main()
{
vector vecF;
Person* p = new Person();
p->age = 30;
Family* f = new Family();
f->houseHold = p;
f->aptNum = 100;
vecF.push_back(*f);
delete f;
delete p;
cout <<
vecF.at(0).houseHold->age << endl << vecF.at(0).aptNum << endl;
getchar();
}

Tuesday, August 21, 2007

Tuesday, August 14, 2007

Get used to managed C++ style definition

Sometimes the ugly managed c++ grammar can cause us much pain.

For example, what is the correct way of defining a __wchar_t gc array?

Wrong answers:
__wchar_t charArr[] = {':', '\\'};
__wchar_t[] charArr = {':', '\\'};
__wchar_t __gc[] charArr = {':', '\\'};
__wchar_t __gc charArr[] = {':', '\\'};
__wchar_t __gc[] charArr[] = {':', '\\'};
JUST_LET_ME_DEFINE_A_DAMN __wchar_t __gc charArr[] = {':', '\\'};

Only this one is right:
__wchar_t charArr __gc[] = {':', '\\'};

Friday, July 20, 2007

What I've learn from COOP

1. Enterprise-scaled project. More stuff to concern: multi-threading, better use on resources(memory etc.), structure flexibility, code reusability, refactoring.

2. Testing. Software engineering knowledge and experience.

3. Documenting code and project. Also improved writting skills.

4. Interpersonal skills. Working with peers and supervisors. Presentation. Book club.

Thursday, July 12, 2007

Avoid Pitfalls

The following code snippet is error-prone:

for (int i = 0; i <>SelectedIndices->Count; i++)
{
/* do something */
}

Why? listView->SelectedIndices->Count may change in the loop. For example, if we delete one item in listView->SelectedIndices collection a time, listView->SelectedIndices->Count will be 1 less than the the value in last loop.

Another common pitfall is, when deleting items in a collection, items after the deleted item are shifted automatically. Therefore, we are to delete some items one by one in a collection according to their initial indices, we need to delete them backwards.

Friday, July 6, 2007

Making Textbox control autoscrollable

If text is appended every time, we can do this way:

txtBx->AppendText(strToAppend);
txtBx->Focus();
txtBx->ScrollToCaret();

If text is completely loaded from the beginning to the end every time, it can be done this way:

txtBx->Text = strContent;
txtBx->Focus();
txtBx->SelectionLength = 0;
txtBx->SelectionStart = txtBx->Text->Length;
txtBx->ScrollToCaret();

In either case, Focus method is indispensable.