您的位置:首页 > 编程语言 > C语言/C++

c++常识要点

2015-05-01 10:22 162 查看
1 指针、常量、的理解

char * const cp : 定义一个指向字符的指针常数

const char* p : 定义一个指向字符常数的指针

char const* p : 等同于const char* p

2 子类对象调用父类的同名函数。举例如下:

class A {

void f1(){};

};

class B : public A {

void f1(){};

}

void main() {

B b;

b.A::f1();//此处调用A的f1()函数

}

3 .c++ primer ed4th 指出,包含头文件时,如果是标准库的文件应该使用尖括号,如果是自定义的文件,应该使用双引号。原文如下:

Headers for the standard library are enclosed in angle brackets (< >). Nonstandard headers are enclosed in double quotes (" ").
4 教程中指出的语句命名空间,在测试时没有起作用。原文如下:
statement names defined within the condition of a statement, such as anif, for, orwhile.
测试例子如下:
for(int i = 0; i < 5; i++) {

cout << i << endl;

}

cout << "integral:" << i << endl;

//out:

//0

//1

//2

//3

//4

//integral:5

5.枚举类型enum的变量enumeration只能被枚举成员enumerator赋值赋值或者其它的同类型对象enumeration赋值。

6.类定义由类接口interface(在public区)和类实现implementation (在private区)组成。数据成员放在类实现区,方法成员部分放在类实现区,部分放在类接口区

7.c++ primer 4th说可以这样定义 size_type类型:

vector<string>::size_type

但是我试过了没有成功,cl编译器报了一堆看不懂的错误。只能使用string::size_type

更正:已经成功使用,需要使用using声明:using std::vector<string>;

8 使用string和unsigned long 分别初始化bitset类模板的差别示例。

string bstr = "101101111";

unsigned int ulong = 367;//等于 101101111

bitset<8> b1(bstr);

bitset<8> b2(ulong);

cout << "b1:" << b1 << endl;

cout << "b2:" << b2 << endl;

//输出:

//10110111

//01101111

9 使用数组初始化向量的示例

const size_t arr_size = 6;

int int_arr[arr_size] = {0, 1, 2, 3, 4, 5};

// ivec has 6 elements: each a copy of the corresponding element in int_arr

vector<int> ivec(int_arr, int_arr + arr_size);

10 const类型对象的初始化

const对象的初始化值要求是确定的值

比如

int i = 50;

const int ic = i;//初始化值50是确定

但是如下就是错误的

int i;

const int ic = i//此时,初始化值不知道是多少,i=50的作用域是【i=50】这一行到函数体末尾。

...

i = 50;

11 使用cout输出二进制

int short_value = 32767;

cout <<hex<< bitset<sizeof(int)*8>(short_value) << endl; //bitset<size_t>(),类模板构造函数

待续
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: