2014年6月20日星期五

My Trap For C++ - CharellkingQu

本邮件内容由第三方提供,如果您不想继续收到该邮件,可 点此退订
My Trap For C++ - CharellkingQu  阅读原文»

1 My Trap For C++

Discovery my fauls in my habit and overcome them.

1.1 Initialize variable

In 2012, at about 1:00am, I still worked in company, suddenly other staff discovevied crashes in server. I checked it, crashes occured when container's iterator isn't initialized; because of mass copying-code, the bugs appears in many places. I rapidly completed all the variable initializaiton.

From then on, when a variable is declared, I must initialize it.

Gernerally, we initialize different types as below:

general type

int a = 0;
double a = 0.0;
char a = '\0';

general type's array

int a[10] = {0};
double f[10] = {0.0};

pointer

int* p_a = NULL;
std::vector<int>* p_a = NULL;

class

class Object {
public:
Object():a(0), b(0.0), c('\0') {
}
private:
int a;
double b;
char c;
};

Certainly you can initialize your variable with other value instead of default value above when your variable is not a undefilized variable.

1.2 Remove when make iteration to container

When making iteration to container, you are removing your container's elements.

for (iter = c.begin(); c.end() != iter; ++iter) {
if (xxx) {
erase(iter);
}
}

Obviously, you can find out the bug as soon as possible. When a function with removing the container's element is called in the "for" body, it isn't obvious for you.

So when crashes appear during the iteration to container, you should check the "for" body carefully.

I list the method of doing iteration when remove elements in the containers as below:

vector, list

std::vector<int>::iterator iter = v.begin();
while (v.end() != iter) {
if (xxx) {
iter = v.erase(iter);
} else {
++iter;
}
}

map, set, multimap, multiset

std::map<int>::iterator iter = rbtree.begin();
while (rbtree.end() != iter) {
if (xxx) {
v.erase(iter++);
} else {
++iter;
}
}

To know all kind of the container's "erase" function, you can refer the website:

http://www.cplusplus.com

1.3 Forget increment or decrement when use "while"

In the recent past, I use mass "while" without increment or decrement, make mass infinite loop. At that time, I lose the faith to myself. I replace "while" with "for" in my code as possible.

for(std::hash_map<int, std::vector<int> >::iterator iter = m.begin(); m.end() != iter; ++iter) {
//TODO
}

Obviousely "for" style reminds me to add increment or decrement. But "for" style's condition is too long, you can break the line and "while" style is cleaner. Now I can overcome the problem, I firstly implement a "while" frame with increment or decrement, then add my code in the "TODO" position.

std::hash_map<int, std::vector<int> >::iterator iter = m.begin();
while (m.end() != iter) {
//TODO
++iter;
}

Tips: When add "continue" in the "TODO" position, after execute the "contine" command, "while" don't make indecrement or decrement, but "for" make it.


本文链接:My Trap For C++,转载请注明。

使用MVVM-Sidekick开发Universal App(一) - yan_xiaodi  阅读原文»

终于要迈进Universal的大坑了,还有点小激动呢。

CurrencyExchanger 掌中汇率是我前几年发布在Windows Phone商店中的一个应用,当时是WP7版本,下载链接:http://www.windowsphone.com/zh-cn/store/app/%E6%8E%8C%E4%B8%AD%E6%B1%87%E7%8E%87free/84e93a20-cefb-460f-b0d9-a57689b33c10

已经很久没有升级了,最近想学习一下Universal开发,就拿这个练练手吧。之前一直没有系统的写过文章,现在从头把开发中的一些过程记录一下,也是对自己的一个促进。因为是边做边写,肯定会有错误,请大家不吝赐教。

一、新建项目

我使用了MVVM-Sidekick框架,这是一个简单但功能强大的MVVM框架,由微软的@韦恩卑鄙 开发,我一直用这个框架开发WP8的程序,前不久作者升级支持了Universal App

新建项目前需要先安装MVVM-SidekickVS扩展插件,在VS2013update2的工具-扩展与更新菜单中搜索mvvm-sidekick就可以找到这个扩展,下载安装即可。安装后会添加项目模板和代码段,比较方便。

githubhttps://github.com/waynebaby/mvvM-Sidekick

vs插件:http://visualstudiogallery.msdn.microsoft.com/ef9b45cb-8f54-481a-b248-d5ad359ec407

现在可以新建项目了,选择通用应用程序,MVVM-Sidekick Universal App项目模板,输入CurrencyExchanger,等待VS建立项目。这个地方有个需要注意,项目名称不能太长,我第一次输入了一个比较长的名字,结果VS提示名称太长,建立失败了。

二、项目结构

现在可以看到VS2013为我们生成了三个项目,

CurrencyExchanger.Windows

CurrencyExchanger.WindowsPhone

CurrencyExchanger.Shared

可以看到我们熟悉的App.xaml文件被放到了Shared项目中,打开看一下,

#if WINDOWS_PHONE_APP
private TransitionCollection transitions;

没有评论:

发表评论