c++笔记2 - webdancer's Blog

c++笔记2

webdancer posted @ 2010年9月28日 10:54 in 编程语言 with tags c++ , 1611 阅读

      现在才觉得当自学的时候,应该看一本好书。先到网上搜一下,再下决定。记得当时学C的时候,看的那本书并没有把C的特点给说明白,对指针也是很含糊。现在看着本书,真的很好。

     1.数组初始化。

如果没有显式的初始化:

》在函数体外定义的内置数组,会进行初始化:0。

》在函数体内定义的内置数组,不进行初始化。

》类类型,不论定义在何处,都自动调用默认构造函数。若没有默认的构造函数,则必须显式的初始化。

     2.数组不能直接赋值,而且一定要保证下标的范围。说起来简单,我就经常犯错误。

     3.const与*。

我以为理解的可以了,一个例子又把我搞晕了。

string s1="hello";
    string s2="world";
    typedef string * pstr;
    const pstr str=&s1;
    *str="world";
    cout<<*str<<endl;    

正常编译,不会出错。原来理解成了:const string * str。关键把typedef的作用理解错了,认为成文本替换了。正确的是:

string * const str。

ps:  The typedef keyword allows you to create a new alias for an existing data type. 

      4.减少c风格string的使用。主要是:减少错误。

     


登录 *


loading captcha image...
(输入验证码)
or Ctrl+Enter
Host by is-Programmer.com | Power by Chito 1.3.3 beta | © 2007 LinuxGem | Design by Matthew "Agent Spork" McGee