1. String问题
#include <iostream>
…
std::out << …
或者
#include <iostream>
using namespace std;
cout <<...
在程序中加入using namespace,就可以不用再程序体内加入很多的std::
在C++中如果用:
#include “iostream.h”
….
std::out<<...
就会报错 ‘std’ is not a class or namespace name
旧标准是:<iostream.h> 不必 using namespace std;
新标准是:<iostream> 要有 using namespace std;
iostream.h 里没有std空间,只有iostream里有。
2. 类型转换问题
将整形转成字符型:
1) sprintf
char buffer[10];
sprintf(buffer,"%f", i*0.1);
注意:如果使用char * buffer;代替buffer[10],这是比较危险的,我之前试过,程序直接退出。该语句只声明分配了一个随机内存不一定放得下你的东西
2) stream
#include <sstream>
int i;
std::string str;
std::stringstream ss;
ss<<i;
ss>>str;
3. 赋初值:下列两种表达是相同的
//
//Rectangle::Rectangle(int width, int height) {
//// TODO Auto-generated constructor stub
//itsWidth = width;
//itsHeight = height;
//}
//The following equals above
Rectangle::Rectangle(int width, int height):
itsWidth(width),itsHeight(height){}
//单冒号好像只出现在constructor中
4.辨别:
new char[i]是分配字符数组,含i个char变量。
new char(i)是分配1个char变量,用i初始化它。
const char* a说明a是个普通指针, 但是指向一个常量char,所以不能通过a来修改指向的char
char* const a说明a是个常量指针,不能改变a指向的目标,但是能通过a修改指向的char
char* const a说明a是个常量指针,不能改变a指向的目标,但是能通过a修改指向的char
5.创建对象两种基本方式:
Array a; //在stack栈中创建了一个Array对象,并分配了内存
Array* a = new Array(); //在heap堆中创建了一个Array对象,new操作返回的是一个地址
6.函数返回对象问题:
1)定义一个对象,
让函数返回对象。如:std::string test(); 返回对象属于值拷贝,相对于引用和指针效率低
2) 函数返回void,把对象作为参数以引用的方式传递给被调函数,
在被调函数内改变所传递对象的值如:
callee function:
void Properties::GetProperty(const char* key, std::string& ref_value){ }
caller function:
int main() {
Properties p("testfile");
std::string s;
p.GetProperty("zhangyang",s);
std::cout<<s.data()<<std::endl;
return 1;
}
3) return a reference
callee function:
void Properties::GetProperty(const char* key, std::string& ref_value){ }
caller function:
int main() {
Properties p("testfile");
std::string s;
p.GetProperty("zhangyang",s);
std::cout<<s.data()<<std::endl;
return 1;
}
3) return a reference
callee function:
std::string& Properties::GetProperty(const char* key){
//必须用new的方式创建一个string所指的对象,不能用String s,这种方式是在堆栈创建对象,被调函数退出后,对象就不存在了,无法返回。
//如果把,new创建的对象赋给引用后,再删除new所创建的对象,会导致什么也返回不了,因为引用只是一个别名,
//在caller函数中释放引用,这样能保证释放new所创建的对象吗?
// //this mode is not right, it is created in stack not heap, it disappear when the func exit;
// std::string str_value;
// str_value.assign(value);
// std::string& ref_value = str_value;
//The following works.
std::string* str_value = new std::string(); //this mode is right,you need delete teh memory
str_value->assign(value);
std::string& ref_value = *str_value;
return ref_value;
}
caller function:
int main() {
Properties p("testfile");
std::string& ref_value=p.GetProperty("zhangyang");
std::cout<<ref_value.data()<<std::endl;
//delete ref_value;//compile error,since reference is just a alias of an object, it doesn't occupy memory.needless delete
//for now, we don't have way to delete str_value created in GetProperty
}
caller function:
int main() {
Properties p("testfile");
std::string& ref_value=p.GetProperty("zhangyang");
std::cout<<ref_value.data()<<std::endl;
//delete ref_value;//compile error,since reference is just a alias of an object, it doesn't occupy memory.needless delete
//for now, we don't have way to delete str_value created in GetProperty
}
4)
return a pointer (应该和3差不多)
7.关于保留字virtual的理解:
This is inded a pretty fancy trick for C++ to pull off.
为什么要声明为virtaul:
1)如果此函数日后可能会被其派生类重载
2)派生类中的其他函数在调用该函数时,调用的是派生类中该函数的实现