Thread: If you declare a local pointer with the same name & type as a pointer data member ...

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Registered User deoren's Avatar
    Join Date
    Mar 2003
    Posts
    63

    If you declare a local pointer with the same name & type as a pointer data member ...

    I'm working on Chapter 8, Exercise 6 from Ivor Horton's Beginning Visual C++ 2008.

    This example uses code written in a previous exercise and builds on it. These are the directions:

    Modify the stack example from Exercise 7 in the previous chapter so that the size of the stack is specified in the constructor and dynamically allocated.
    Here's the original header file contents for Chapter 7, Exercise 7:

    Code:
    class CStack
    {
    public:
       CStack() : next(0) {}
       void push(int i);
       int pop();
       void print();
    private:
       int list[100];
       int next;
    };
    Here's my current modified copy of it with all comments removed (also here):

    Code:
    class CStack
    {
    public:
    
        CStack(int requested_size = 1);
        CStack(const CStack& s);
        ~CStack();
    
        CStack& operator=(const CStack& rhs);
    
        void push(int value);
        int pop();
        void print();
    
    private:
        int* plist;
        int next;
        int stack_size;
    };

    After comparing my code against the author's solution, I realized I went a bit overboard and ended up with something else other than what the author intended for the reader to create.

    Inside of the push() member function there is a check for whether the max capacity for the stack has been reached. If not, the next available slot (as I've taken to calling it) is used to store a value.

    If there is no more room, the stack is "grown" by:

    1. Creating a temporary dynamic array
    2. Copying the existing array to the temporary
    3. Copying new value (argument to pop() function) to temporary
    4. Deleting the existing array
    5. Creating a new one sized to the temporary array
    6. Copying everything back over
    7. Deleting the temporary



    I'm sure that's enough to make anyone of skill cringe, but I'm new, so I'm not beating myself up too much.

    While working on this exercise, one mistake I know I made was declare a pointer within pop() with the same name as a data member pointer. I didn't realize I had done it until g++ gave me a warning that the locally declared variable wasn't being used.

    Once I realized what I did, I changed all usages of plist to this->plist to make it stand out more (to me anyway).

    Code:
    -            int* plist = new int[new_stack_size];
    +            this->plist = new int[new_stack_size];
    So, all of that said, did my declaration of plist within pop() mask or hide this->plist?





    P.S.

    If I wasn't clear, I'm referring to when this line:

    Code:
    this->plist = new int[new_stack_size];
    was

    Code:
    int* plist = new int[new_stack_size];
    when I got the warning.

    Thanks in advance for any help!

    Edit:

    The original code (if anyone is interested) can be found here:

    http://www.wrox.com/WileyCDA/WroxTit...-DOWNLOAD.html
    Last edited by deoren; 01-21-2012 at 06:10 PM. Reason: Added link to author's original code
    It is better to fail with honor than win by deceit
    - unknown

    My erratic tinkerings:
    http://projects.whyaskwhy.org/

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. static pointer data member
    By suppu143 in forum C++ Programming
    Replies: 2
    Last Post: 03-14-2011, 07:46 AM
  2. Replies: 9
    Last Post: 06-13-2009, 02:31 AM
  3. Replies: 5
    Last Post: 04-04-2009, 03:45 AM
  4. Pointer to array as member data...
    By JulesGlass in forum C++ Programming
    Replies: 6
    Last Post: 04-19-2008, 08:42 AM
  5. Replies: 4
    Last Post: 08-27-2007, 11:51 PM

Tags for this Thread