Trying to understand multiple pointers

# Thread: Trying to understand multiple pointers

1. ## Trying to understand multiple pointers

Hello, I'm trying to grasp the concept of multiple pointers in C++.

For example, say we have a:

class A () { foo; }

...and later on in the program we have:

A* function1();

A** &operator[](char ) const {}

A*** var;

Now is the succession like this:

function1 --&gt; &operator[] --&gt; var

...or vice-versa?

At this point, the more I try to figure it out the more I'm asking, "Who's on first? What's on second, etc"

2. Hi

My first suggestion would be to actually implement a valid class and try the (class) pointer
constructs you are interested in. I do not really understand the examples as you have
thrown them in here. I recommend you to first

understand the concept of pointers[1]
understand the passing of functions within functions[2]

Then again, create a small (compileable) program we can look at and discuss
more specific.

Cheers.

[2] Example: (sorry no reference found)
Code:
```void function_passing( int flag, double (*pt2Func)(double a, double b) );
...
double multiplication(double c, double d){ return c*d;}
...
function_passing(1,&multiplication);```

3. Hello, I'm trying to grasp the concept of multiple pointers in C++.

For example, say we have a:

class A () { foo; }

...and later on in the program we have:

A* function1();

A** &operator[](char ) const {}

A*** var;

Now is the succession like this:

function1 --&gt; &operator[] --&gt; var

...or vice-versa?

At this point, the more I try to figure it out the more I'm asking, "Who's on first? What's on second, etc"
If I understand this correctly, function1 --&gt; &operator[] --&gt; var is not valid, here is why:

function1 is a function that returns a pointer to an object of class A, since function1 itself is not a pointer, so you would not derefernce function1. Again I could be wrong but this is my take.

You might use it like this:

A* pRet = function1();
cout << pRet->m_Value;

That is if the class A had a member m_Value, since pRet is a pointer to an A object.

A** &operator[](char ) const {}

Firstly, this is an operator overload. This is usualy done with a container class or a class that contains an array of some sort. Normaly, operator[] is a member of the class. Aditionaly the parameter char is usualy an int, used as an index for the array element that the user wishes to refernce. A common example: (not tested)

Code:
```class A
{
public:
A** &operator[](int index) const { return m_Array[index]; }

private:
A** m_Array[];
};```
A*** var;

looks like var is indeed a triple pointer to an object of type A, see the following

http://www.experts-exchange.com/Prog..._21353282.html

I hope this helps, and that I didnt confuse you further. Also please feel to correct me if I am wrong, anyone.

4. If you are getting that much indirection, you are probably working with a poor design and need to rethink what you are trying to do.

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•