Item08. 多级指针(Pointers to Pointers)
2005-04-10 19:49
330 查看
Item08. Pointers to Pointers
指向指针的指针(多级指针)与指针有相同的地方也有不同的地方
--------------------------------------------
1、改变指针的地址指向
void scanTo( const char **p, char c ) {
while( **p && **p != c )
++*p;
}
char s[] = "Hello, World!";
const char *cp = s;
scanTo( &cp, ',' );//指向","
2,优先使用指针的引用
void scanTo( const char *&p, char c ) {
while( *p && *p != c )
++p;
}
3,指针与多级指针的差别
1)指针能用于多态,而多级指针不可以
Circle *c = new Circle;
Shape *s = c; // 正确...
Circle **cc = &c;
Shape **ss = cc; // 错误!
2)加上const搅合后,非指针常量可以赋值给指针常量,但不可以赋值给多级指针
char *s1 = 0;
const char *s2 = s1; // 正确...
char *a[MAX]; // aka char **
const char **ps = a; // 错误!
指向指针的指针(多级指针)与指针有相同的地方也有不同的地方
--------------------------------------------
1、改变指针的地址指向
void scanTo( const char **p, char c ) {
while( **p && **p != c )
++*p;
}
char s[] = "Hello, World!";
const char *cp = s;
scanTo( &cp, ',' );//指向","
2,优先使用指针的引用
void scanTo( const char *&p, char c ) {
while( *p && *p != c )
++p;
}
3,指针与多级指针的差别
1)指针能用于多态,而多级指针不可以
Circle *c = new Circle;
Shape *s = c; // 正确...
Circle **cc = &c;
Shape **ss = cc; // 错误!
2)加上const搅合后,非指针常量可以赋值给指针常量,但不可以赋值给多级指针
char *s1 = 0;
const char *s2 = s1; // 正确...
char *a[MAX]; // aka char **
const char **ps = a; // 错误!
相关文章推荐
- Item08. 多级指针(Pointers to Pointers)
- Item08. 多级指针(Pointers to Pointers)
- Item07. 常量与指针的联姻(Const Pointers and Pointers to Const)
- 指向类成员的指针 (pointers to class members)
- const Pointers and Pointer to const Objects 常量指针和指向常量对象的指针
- Item07. 常量与指针的联姻(Const Pointers and Pointers to Const)
- Thinking in C++: Pointers to members 指向成员的指针
- Smart pointers to boost your code(智能指针)
- const Pointers and Pointer to const Objects 常量指针和指向常量对象的指针
- Item07. 常量与指针的联姻(Const Pointers and Pointers to Const)
- Pointers to Class Members:指向类成员的指针
- 指针的指针(pointers to pointers)
- 指向函数的指针(pointers to functions)和回调函数
- Thinking in C++: Pointers to members 指向成员的指针
- [C++]C++ Pointers to functions 函数指针
- [C++]C++Pointers to classes 类指针 new 与object的对比
- 指向类成员的指针(Pointers to Class Members)
- Pointers to Class Members Are Not Pointers(指向类成员函数的“指针”并非指针)
- Pay attention to pointers: always initialize your pointers
- C指针学习(Pointers in C)