pointers - To what use is multiple indirection in C++? -
under circumstances might want use multiple indirection (that is, chain of pointers in foo **
) in c++?
most common usage @aku pointed out allow change pointer parameter visible after function returns.
#include <iostream> using namespace std; struct foo { int a; }; void createfoo(foo** p) { *p = new foo(); (*p)->a = 12; } int main(int argc, char* argv[]) { foo* p = null; createfoo(&p); cout << p->a << endl; delete p; return 0; }
this print
12
but there several other useful usages in following example iterate array of strings , print them standard output.
#include <iostream> using namespace std; int main(int argc, char* argv[]) { const char* words[] = { "first", "second", null }; (const char** p = words; *p != null; ++p) { cout << *p << endl; } return 0; }
Comments
Post a Comment