上一页 1 ··· 6 7 8 9 10 11 12 13 14 ··· 21 下一页

2012年10月31日

c++类型转换

摘要: const_cast最普通的用途就是转换掉对象的 const 属性。class Widget { ... }; class SpecialWidget: public Widget { ... }; void update(SpecialWidget *psw); SpecialWidget sw; // sw 是一个非 const 对象。 const SpecialWidget& csw = sw; // csw 是 sw的一个引用 它是一个 const 对象 update(&csw); // 错误!不能传递一个 const SpecialWidget*... 阅读全文

posted @ 2012-10-31 10:32 GIS-MAN 阅读(156) 评论(0) 推荐(0) 编辑

2012年10月30日

指针与引用的区别

摘要: string& rs; // 错误,引用必须被初始化 string s("xyzzy"); string& rs = s; // 正确,rs指向 s 指针没有这样的限制。 string *ps; // 未初始化的指针 // 合法但危险指针与引用的另一个重要的不同是指针可以被重新赋值以指向另一个不同的对象。 但是引用则总是指向在初始化时被指定的对象,以后不能改变。string s1("Nancy"); string s2("Clancy"); string& rs = s1; // rs ... 阅读全文

posted @ 2012-10-30 20:01 GIS-MAN 阅读(176) 评论(0) 推荐(0) 编辑

c++ 参数传递 之引用形参

摘要: 在函数内部对引用形参做 出的改变会影响到函数外部实参的改变引用形参的例子void swap(int &v1, int &v2) { int tmp = v2; v2 = v1; v1 = tmp; } 非引用形参void swap(int v1, int v2) { int tmp = v2; v2 = v1; // assigns new value to local copy of the argument v1 = tmp; }使用引用... 阅读全文

posted @ 2012-10-30 17:29 GIS-MAN 阅读(611) 评论(0) 推荐(0) 编辑

c++ 参数传递之 非引用形参

摘要: 1 非引用实参每次调用函数时,都会重新创建该函数所有的形参,此时所传递的实参将会初始化对应的形参。 形参的初始化与变量的初始化一样:如果形参具有非引用类型,则复制实参的值,如果形参为引用类型(第 2.5 节),则它只是实参的别名。指针形参————————————————可以修改实参指针指向的值,但是不能修改实参指针函数的形参可以是指针(第 4.2 节),此时将复制实参指针,如果函数形参是非 const 类型的指针,则函数可通过指针实现赋值,修改指针所指向对象的值:void reset(int *ip) { *ip = 0; // changes the value ... 阅读全文

posted @ 2012-10-30 17:20 GIS-MAN 阅读(370) 评论(0) 推荐(0) 编辑

c++ 枚举

摘要: 枚举成员值可以是不唯一的。 // point2d is 2, point2w is 3, point3d is 3, point3w is 4 enum Points { point2d = 2, point2w, point3d = 3, point3w }; 本例中,枚举成员 point2d 显式初始化为 2。下一个枚举成员 point2w 默认初始化,即它的值比前一枚举成员的值大 1。因此 point2w 初始化为 3。枚举成员 point3d 显式初始化为 3。一样,point3w 默认初始化,结果为 4。枚举的使用::Points pt3d = point3d; ... 阅读全文

posted @ 2012-10-30 16:47 GIS-MAN 阅读(164) 评论(0) 推荐(0) 编辑

c++ const

摘要: Const int bufSize=512// bufSize 以后不能修改,定义时必须初始化const std::string hi=“helloworld”在全局作用域声明的 const 变量是定义该对象的文件的局部变量。此变量只存在于那个文件中,不能被其他文件访问。通过指定 const 变更为 extern,就可以在整个程序中访问 const 对象:// file_1.cc // defines and initializes a const that is accessible to other files extern const int bufSize =... 阅读全文

posted @ 2012-10-30 16:42 GIS-MAN 阅读(170) 评论(0) 推荐(0) 编辑

c++ arrow operator ,, –> Operator,,,指针专属

摘要: C++ has an operator that can be used with a pointer to simplify the notation for specifying the members of a struct or a class. The arrow operator , -> , combines the actions of a dereferencing operator, * , and a dot operator to specify a member of a dynamic struct or class object that is p... 阅读全文

posted @ 2012-10-30 14:52 GIS-MAN 阅读(799) 评论(0) 推荐(0) 编辑

让c++ 函数返回一个数组

摘要: 在c++中是不允许数组作为函数的返回值的int [] someFunction( ); //ILLEGAL要想实现函数返回一个数组,那返回对应数组里面类型的指针you must return a pointer to the array base type and have the pointer point to the array. So, the function declaration would be as follows: int* someFunction( ); //Legalint* doubler( int a[], int size){int* temp = new in 阅读全文

posted @ 2012-10-30 14:45 GIS-MAN 阅读(45763) 评论(0) 推荐(0) 编辑

c++ const 修饰数组

摘要: //直接声明为 int a[], 这样会允许函数内部对a[] 进行修改void showTheWorld( int a[], int sizeOfa) { for ( int i = 0; i < sizeOfa; i++) cout << a[i] << " "; cout << endl; }如果 声明为const int a[], C++就不允许函数内部在对a[] ,进行修改了 void showTheWorld( const int a[], int sizeOfa) { ... 阅读全文

posted @ 2012-10-30 11:39 GIS-MAN 阅读(4796) 评论(0) 推荐(0) 编辑

c++ 数组

摘要: 声明: int a[6];声明时初始化 :允许不给数组的数量 int b[] = {5, 12, 11}; is equivalent to int b[3] = {5, 12, 11};函数里的数组: 阅读全文

posted @ 2012-10-30 11:03 GIS-MAN 阅读(147) 评论(0) 推荐(0) 编辑

上一页 1 ··· 6 7 8 9 10 11 12 13 14 ··· 21 下一页

导航