27 September
在STL列表(vector)中插入不同类型的对象(指针)
最近在做的东西里有个要求就是最好能把不同类型的对象存在一个列表里,而且这些类型有个共同点就是有个相同的基类。大家都会想到用容器,然后在里面存对象的指针。这方面网上也有很多资料,其中的麻烦都在指针的使用上。我自己也做了个例程,在VC.NET下编译运行通过。最后示例了一个泛型方法find。
//////////////////////////////////////////////////////////////////////////
#pragma once
#include <iostream>
#include <vector>
#include <algorithm>
#include <string>
using namespace std;
//////////////////////////////////////////////////////////////////////////
//父类
class Base
{
public :
Base();
string GetMyClassName();
virtual string GetDescription();
string myClassName;
string description;
};
Base::Base() { myClassName = "In the Base::Base()";}
string Base::GetMyClassName() { return myClassName;}
string Base::GetDescription()
{
description = "In the Base::GetDescription()";
return description;
}
//////////////////////////////////////////////////////////////////////////
//子类A
class DeriveA : public Base
{
public :
string myDeriveA;
DeriveA();
void SetMyDeriveA();
string GetMyDeriveA();
virtual string GetDescription();
};
DeriveA::DeriveA() { myClassName = "In the DeriveA::DeriveA()";}
void DeriveA::SetMyDeriveA() { myDeriveA = "In the DeriveA::SetMyDeriveA()";}
string DeriveA::GetMyDeriveA() { return myDeriveA;}
string DeriveA::GetDescription()
{
description = "In the DeriveA::GetDescription()";
return description;
}
//////////////////////////////////////////////////////////////////////////
//子类B
class DeriveB : public Base
{
public :
string myDeriveB;
DeriveB();
void SetMyDeriveB();
string GetMyDeriveB();
virtual string GetDescription();
};
DeriveB::DeriveB() { myClassName = "In the DeriveB::DeriveB()";}
void DeriveB::SetMyDeriveB() { myDeriveB = "In the DeriveB::SetMyDeriveB()";}
string DeriveB::GetMyDeriveB() { return myDeriveB;}
string DeriveB::GetDescription()
{
description = "In the DeriveB::GetDescription()";
return description;
}
//////////////////////////////////////////////////////////////////////////
//主程序
int main()
{
//生成对象
Base* myBase = new Base;
DeriveA* myDeriveA = new DeriveA;
myDeriveA->SetMyDeriveA();
DeriveB* myDeriveB = new DeriveB;
myDeriveB->SetMyDeriveB();
//插入对象
vector<Base *> MyVector;
MyVector.empty();
MyVector.push_back(myBase);
MyVector.push_back(myDeriveA);
MyVector.push_back(myDeriveB);
//调用对象
vector<Base *>::iterator ThisVector = MyVector.begin();
cout << "Let's Go !:" << endl;
int i = 0;
while( ThisVector != MyVector.end() )
{
cout << i <<endl;
//cout << (**ThisVector).GetMyClassName() << endl; //一般像下面这么写
cout << (*ThisVector)->GetMyClassName() << endl; //(*ThisVector)的括号不能省,优先级问题
cout << (*ThisVector)->GetDescription() << endl;
if ( (*ThisVector)->GetMyClassName().compare("In the DeriveA::DeriveA()") == 0)
{
cout << ((DeriveA*)*ThisVector)->GetMyDeriveA() << endl;
}
if ( (*ThisVector)->GetMyClassName().compare("In the DeriveB::DeriveB()") == 0)
{
cout << (*((DeriveB*)*ThisVector)).GetMyDeriveB() << endl;
//这是另外一种写法,效果和上面一样的,就是不够简洁
}
ThisVector++;
i++;
}
//find方法
cout << endl;
cout << "Can I find DeriveA ?" << endl;
ThisVector = find(MyVector.begin(), MyVector.end(), myDeriveA);
if (ThisVector != MyVector.end())
{
cout << (*ThisVector)->GetMyClassName() << endl;
((DeriveA*)*ThisVector)->SetMyDeriveA();
cout << ((DeriveA*)*ThisVector)->GetMyDeriveA() << endl;
cout << (*ThisVector)->GetDescription() << endl;
cout << "HO HO !! Certainly, You can !" << endl;
}
return 0;
}
12 September
VC调试方法
大部分是转载的网上别人的资料,俺核对了一下正确性,然后把中英文版对照一下
调试快捷键
F5: 开始调试
Shift+F5: 停止调试
F10: 调试到下一句,不进入函数内部
F11: 调试到下一句,跟进到有代码的函数内部
Shift+F11:从当前函数中跳出
Ctrl+F10: 调试到光标所在位置
F9: 设置(取消)断点
Alt+F9: 高级断点设置
跟踪调试
1.尽量使用快捷键进行调试。
2.观察调试信息。
3.高级中断设置。
异常调试
重试->取消->调试。
函数堆栈,用variables或者call stack窗口。
Release调试
1.经常测试你的Debug和Release版本。
2.不要移除调试代码,如ASSERT, TRACE等。
3.初始化变量,特别是全局变量,malloc的内存,new的内存。
4.当你移除某个资源是,确保你移除了所有跟这个资源相关的申明(主要是在resouce.h文件中)。
5.使用3或者4级的警告级编译你的代码,并确保没有警告,project->setting->c/c++ ->warning level(中文版是项目-〉属性-〉C/C++-〉常规-〉警告等级)。
6._debug改成NDEBUG进行调试,project->setting->c/c++ ->Preprocessor definitions(中文版是项目-〉属性-〉C/C++-〉预处理器-〉预处理器定义)(这里是debug和Release编译的重要不同之一)。
7.在Release中调试源代码,project->setting->c/c++ -> debug info选择programDatabase(中文版是项目-〉属性-〉C/C++-〉常规-〉调试信息格式-〉用于“编辑并继续”的程序数据库),project->setting->link 选上Generate debug info(中文版是项目-〉属性-〉链接器-〉调试-〉生成调试信息)。
8.走读代码,特别关注堆栈和指针。
23 August
学习之路
01.《Essential C++》 //适合初雪者,可看可不看
02.《c++ primer》 //一定要看,学习C++全部语法知识
03.《Effective_C++》 //c++重点
04.《标准C++ 输入输出流与本地化》 //iostream
05.《深度探索C++对象模型》 //难!
06.《C++STL中文版》 //STL简易版,一定要看,这几年就靠STL吃饭了
07.《C++标准程序库》 //STL_step 1
08.《泛型编程与STL》 //SLT_step 2
09.《STL源码剖析--侯捷》 //STL_step 3
10.《高质量C++编程指南》 //完成,有时间还要拿来多看看
11.《windows程序设计》 //一定要好好看看,人在windows的屋檐下,不得不低头
12.《设计模式》 //正在看,为了一种境界,如果把编程当作艺术的话
资料库
01.《C++头文件库》 //都是吃饭的家伙,用到再翻
02.《c++函数库》
03.《C函数库》
课外读物
01.《C++沉思录》 //听说都写得不错,有时间就当小说看啦
02.《C++批判(第三版)》
03.《C++语言设计和演化》
看书原则是:先中文后英文,看过中文不看英文