博文

学C++的50条原则(2006-12-20 18:55:00)

摘要:1.把C++当成一门新的语言学习(和C没啥关系!真的。);

2.看《Thinking In C++》,不要看《C++变成死相》;

3.看《The C++ Programming Language》和《Inside The C++ Object Model》,不要因为他们很难而我们自己是初学者所以就不看;

4.不要被VC、BCB、BC、MC、TC等词汇所迷惑——他们都是集成开发环境,而我们要学的是一门语言;

5.不要放过任何一个看上去很简单的小编程问题——他们往往并不那么简单,或者可以引伸出很多知识点;

6.会用Visual C++,并不说明你会C++;

7.学class并不难,template、STL、generic programming也不过如此——难的是长期坚持实践和不遗余力的博览群书;

8.如果不是天才的话,想学编程就不要想玩游戏——你以为你做到了,其实你的C++水平并没有和你通关的能力一起变高——其实可以时刻记住:学C++是为了编游戏的;

9.看Visual C++的书,是学不了C++语言的;

10.浮躁的人容易说:XX语言不行了,应该学YY;——是你自己不行了吧!?

11.浮躁的人容易问:我到底该学什么;——别问,学就对了;

12.浮躁的人容易问:XX有钱途吗;——建议你去抢银行;

13.浮躁的人容易说:我要中文版!我英文不行!——不行?学呀!

14.浮躁的人容易问:XX和YY哪个好;——告诉你吧,都好——只要你学就行;

15.浮躁的人分两种:a)只观望而不学的人;b)只学而不坚持的人;

16.把时髦的技术挂在嘴边,还不如把过时的技术记在心里;

17.C++不仅仅是支持面向对象的程序设计语言;

18.学习编程最好的方法之一就是阅读源代码;

19.在任何时刻都不要认为自己手中的书已经足够了;

20.请阅读《The Standard C++ Bible》(中文版:标准C++宝典),掌握C++标准;

21.看得懂的书,请仔细看;看不懂的书,请硬着......

阅读全文(2848) | 评论:0

反序字符串的经典例程(2006-11-02 17:43:00)

摘要:#include <stdio.h>
#include <iostream.h> void inverse(char *p)
{
    if( *p == '\0' )
  return;
    inverse( p+1 );
    printf( "%c", *p );
}
int main()
{
 char str[100];
 cout<<"Input your string:"<<endl;
 gets(str);
    inverse(str);
 getchar();
 return 0;
}......

阅读全文(3984) | 评论:0

格式字符串总结(2006-10-31 20:04:00)

摘要: 格式 说明 %o(此处是字母o,不是数字0) 八进制整数 %x OR %X 十六进制整数 %u 无符号整型 %ld Long int %e OR %E 指数格式显示浮点数 %+ “+”表示在数值前有正负号 %# “#”表示在八或十六进制前有合适的前缀 %5d 表示显示的数值最小宽度为5,不足处空格补足。但如果要显示的数值宽度大于5,则按实际的需要输出 %05d 0表示不足的地方用0补足,5解释同上 %8.2f 输出的浮点数最小宽度为8,小数点右边为2位 %12.1e 同上 %- 缺省状态下,printf的输出形式是右对齐的,即会在输出的前边用空格补足,“-”表示使输出为左对齐。......

阅读全文(3868) | 评论:0

[C++] 4种类型转化方法(2006-10-30 12:12:00)

摘要:一、C 风格(C-style)强制转型如下:     (T) expression // cast expression to be of type T 
    函数风格(Function-style)强制转型使用这样的语法:
    T(expression) // cast expression to be of type T
    这两种形式之间没有本质上的不同,它纯粹就是一个把括号放在哪的问题。我把这两种形式称为旧风格(old-style)的强制转型。    二、 C++的四种强制转型形式:   C++ 同时提供了四种新的强制转型形式(通常称为新风格的或 C++ 风格的强制转型):
  const_cast(expression)
  dynamic_cast(expression)
  reinterpret_cast(expression)
  static_cast(expression)   每一种适用于特定的目的:   dynamic_cast 主要用于执行“安全的向下转型(safe downcasting)”,也就是说,要确定一个对象是否是一个继承体系中的一个特定类型。它是唯一不能用旧风格语法执行的强制转型,也是唯一可能有重大运行时代价的强制转型。
    
     static_cast 可以被用于强制隐型转换(例如,non-const 对象转型为 const 对象,int 转型为 double,等等),它还可以用于很多这样的转换的反向转换(例如,void* 指针转型为有类型指针,基类指针转型为派生类指针),但是它不能将一个 const 对象转型为 non-const 对象(只有 const_cast 能做到),它最接近于C-style的转换。
   
   const_cast 一般用于强制消除对象的常量性。它是唯一能做到这一点的 C++ 风格的强制转型。    reint......

阅读全文(3378) | 评论:0

sizeof()用法汇总(2006-10-29 23:22:00)

摘要: sizeof()功能:计算数据空间的字节数
1.与strlen()比较
      strlen()计算字符数组的字符数,以"\0"为结束判断,不计算为'\0'的数组元素。
      而sizeof计算数据(包括数组、变量、类型、结构体等)所占内存空间,用字节数表示。
2.指针与静态数组的sizeof操作
      指针均可看为变量类型的一种。所有指针变量的sizeof 操作结果均为4。
注意:int *p; sizeof(p)=4;
                  但sizeof(*p)相当于sizeof(int);      
      对于静态数组,sizeof可直接计算数组大小;
      例:int a[10];char b[]="hello";
              sizeof(a)等于4*10=40;
              sizeof(b)等于6;
 注意:数组做型参时,数组名称当作指针使用!!
               void  fun(char p[])
  ......

阅读全文(29575) | 评论:9

c++中char * 和 char []的区别(2006-10-24 15:36:00)

摘要:问题引入:
同样char *c = "abc"和char c[]="abc",前者改变其内容程序是会崩溃的,而后者完全正确。
程序演示:
测试环境Devc++
代码
#include <iostream>
using namespace std; main()
{
   char *c1 = "abc";
   char c2[] = "abc";
   char *c3 = ( char* )malloc(3);
   c3 = "abc";
   printf("%d %d %s\n",&c1,c1,c1);
   printf("%d %d %s\n",&c2,c2,c2);
   printf("%d %d %s\n",&c3,c3,c3);
   getchar();
}  
运行结果
2293628 4199056 abc
2293624 2293624 abc
2293620 4199056 abc 参考资料:
首先要搞清楚编译程序占用的内存的分区形式:
一、预备知识—程序的内存分配
一个由c/C++编译的程序占用的内存分为以下几个部分
1、栈区(stack)—由编译器自动分配释放,存放函数的参数值,局部变量的值等。其操作方式类似于 数据结构中的栈。
2、堆区(heap)—一般由程序员分配释放,若程序员不释放,程序结束时可能由OS回收。注意它与数据 结构中的堆是两回事,分配方式倒是类似于链表,呵呵。
3、全局区(静态区)(static)—全局变量和静态变量的存储是放在一块的,初始化的全局变量和静态 变量在一块区域,未初始化的全局变量和未初始化的静态变量在相邻的另一块区域。程序结束后由系统 释放。
4、文字常量区—常量字符串就是放在这里的。程序结束后由系统释放。
5、程序代码区
这是一个前辈写的,非常详细
//main.cpp
  ......

阅读全文(5138) | 评论:0

C++:14,Inheritance & Composition(2006-10-21 21:31:00)

摘要:1.       Code reuse in C++: l         Create objects of your existing class inside the new class. This is called composition because the new class is composed of objects of existing classes. l         Create a new class as a type of an existing class. You literally take the form of the existing class and add code to it, without modifying the existing class. This magical act is called inheritance, and most of the work is done by the compiler. 2.       When you inherit, you are saying, “This new class is like that old class.” You state this in code by giving the name of the class as usual, but before the opening brace of the class body, you put a colon and the name of the base class (or base classes, separated by commas, for multiple inheritance). When you do this, you automatically get all the data members and member functions in the base class......

阅读全文(2643) | 评论:0

Thinking in C++:5 Implementation(2006-10-21 21:17:00)

摘要:1.         If you want to explicitly grant access to a function that isn’t a member of the current structure, this is accomplished by declaring that function a friend inside the structure declaration. It’s important that the friend declaration occurs inside the structure declaration because you (and the compiler) must be able to read the structure declaration and see every rule about the size and behavior of that data type. And a very important rule in any relationship is, “Who can access my private implementation?” 2.        You can declare a global function as a friend, and you can also declare a member function of another structure, or even an entire structure, as a friend. 3.        Making a structure nested doesn’t automatically give it access to private members. To accomplish this, you must follow a particular form: l         fir......

阅读全文(2393) | 评论:0

Thinking in C++:3 The C in C++(2006-10-21 21:01:00)

摘要:1.       The enum keyword (from C) automatically enumerates any list of identifiers you give it by assigning them values of 0, 1, 2, etc. You can declare enum variables (which are always represented as integral values). The declaration of an enum looks similar to a struct declaration. 2.       You’ll notice this in particular if you have an instance of an enumeration color called a. In C you can say a++, but in C++ you can’t. This is because incrementing an enumeration is performing two type conversions, one of them legal in C++ and one of them illegal. First, the value of the enumeration is implicitly cast from a color to an int, then the value is incremented, then the int is cast back into a color. In C++ this isn’t allowed, because color is a distinct type and not equivalent to an int. 3.       Anytime you place a value in a union, the value always starts in the same place at the beginning of......

阅读全文(4310) | 评论:0

malloc()和calloc()的区别(2006-10-17 11:37:00)

摘要:Both the malloc() and the calloc() functions are used to allocate dynamic memory. Each operates slightly different from the other. malloc() takes a size and returns a pointer to a chunk of memory at least that big: void *malloc( size_t size ); malloc()和calloc()都是用来分配动态内存的函数,两者的操作有以下的区别: malloc()以分配的内存大小size为参数返回一个指针,该指针指向一块最小值为size的内存区域。 calloc() takes a number of elements, and the size of each, and returns a pointer to a chunk of memory
at least big enough to hold them all:void *calloc( size_t numElements, size_t sizeOfElement ); calloc的参数为一组元素和每个元素的size,返回的指针指向的内存至少可以存储所有的这些元素单元。 There are one major difference and one minor difference between the two functions. The major difference is that malloc() doesn't initialize the allocated memory. The first time malloc() gives you a particular chunk of memory, the memory might be full of zeros. If memory has been allocated, freed, and reallocated, it probably has whatever junk was left in it. That means, unfortunately, that a progra......

阅读全文(7041) | 评论:2