Results 1 to 4 of 4

Thread: Trying to understand multiple pointers

  1. #1
    Junior Member
    Join Date
    Mar 2005
    Posts
    1

    Unhappy 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 --> &operator[] --> 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. #2
    Senior Member
    Join Date
    Mar 2004
    Posts
    557
    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]
    understand the overloading of operators[3]

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

    Cheers.

    [1] http://cplus.about.com/od/beginnerct.../aa040702a.htm

    [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] http://cplus.about.com/od/beginnerct.../aa101302b.htm
    If the only tool you have is a hammer, you tend to see every problem as a nail.
    (Abraham Maslow, Psychologist, 1908-70)

  3. #3
    Junior Member
    Join Date
    Dec 2004
    Posts
    28
    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 --> &operator[] --> 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 --> &operator[] --> 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.
    My Blog -> journy101.myblogsite.com/blog

  4. #4
    Ninja Code Monkey
    Join Date
    Nov 2001
    Location
    Washington State
    Posts
    1,027
    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.
    "When I get a little money I buy books; and if any is left I buy food and clothes." - Erasmus
    "There is no programming language, no matter how structured, that will prevent programmers from writing bad programs." - L. Flon
    "Mischief my ass, you are an unethical moron." - chsh
    Blog of X

Posting Permissions

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