开发者

Pointer to Pointer to Pointer [duplicate]

开发者 https://www.devze.com 2023-02-03 19:31 出处:网络
This question already has answers here: Closed 12 years ago. Possible Duplicate: Uses for multiple levels of pointer dereferences?
This question already has answers here: Closed 12 years ago.

Possible Duplicate:

Uses for multiple levels of pointer dereferences?

I was reading another 开发者_运维技巧post and this led me to this question. What the heck would something like this mean? Also how deep do people go with a pointer to a pointer to a pointer to a pointer.... I understand Pointer to Pointer but why else you would go more after that? how deep have you gone in using ****?

Foo(SomePtr*** hello);


You could refer to a 3 dimensional array of ints as int *** intArray;


It is rare in C++ certainly.

In C it may well show up where:

  • You use "objects" which are structs, and you always pass them around or create them on the heap as pointers.
  • You have collections of such pointers as dynamically allocated arrays thus T** where T is the type.
  • You want to get an array so you pass in a T*** and it populates your pointer with a T** (array of T* pointers).

This would be valid C but in C++:

  • The first step would be the same. You still allocate the objects on the heap and have pointers to them.
  • The second part would vary as you would use vector and not arrays.
  • The 3rd part would vary as you would use a reference not a pointer. Thus you would get the vector by passing in vector<T*>& (or vector<shared_ptr<T> >& not T***


Well, if your function needs to modify a pointer to a pointer ... ;)

See http://c2.com/cgi/wiki?ThreeStarProgrammer for a discussion.


Pointer to a dynamic array of pointers maybe? Make sense to me.


I never go beyond 'pointer to pointer' - I think if you have to then something is fundamentally wrong with your design.


I think the root cause for this is thinking that "All the problems in the computer science can be solved by one more level of indirection." http://www.c2.com/cgi/wiki?OneMoreLevelOfIndirection

Added level of indirection are often necessary for invoking sub classes methods or directly accessing sub classes elements. And there is no such limit on having pointer to pointer to ...pointer to stuff.

0

精彩评论

暂无评论...
验证码 换一张
取 消