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

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Quote Originally Posted by deoren View Post
    Just to make sure I have this right, the reason this is okay to do is because we're not losing track of any dynamically assigned memory. instead we're just passing the address around to a pointer that we know will free the memory later (class data member with destructor written to explicitly delete it)?
    You got the plot.


    Couldn't I could have left off the this-> portion as long as I didn't create another local plist variable? Something about an implicit this-> being used?
    To be clear, "this->plist" is an unambigious way to write down the class member plist and do something with it. In most situations, omitting "this->" is your prerogative, but you introduced a local variable named plist in some function. That means that "plist" now refers to the local variable instead of the class member. It is a FAR better practice to make sure that class members have descriptive and unique names. I sometimes prefix "my", or a leading underscore (which can be troublesome, since the standard reserves all names _^ where ^ is a capital), but you can really use any little thing.

    Quote Originally Posted by Elysia
    subfixing
    The word is suffix.
    Last edited by whiteflags; 01-22-2012 at 08:13 AM.

  2. #2
    Registered User deoren's Avatar
    Join Date
    Mar 2003
    Posts
    63
    Thanks for the great replies!

    Quote Originally Posted by deoren View Post
    Should the temporary pointer be explicitly deleted instead of relying on the end of the function call to remove it?
    To answer that question, no, you should not do that. When I did so (cstack.cpp, r18) I was thinking of getting rid of the variable itself, but instead I was freeing up the memory I had just purposely reserved for the updated array (and now assigned to this->plist). My best guess is that I should allow the tmp_stack pointer to pass out of scope and be removed like an ordinary local variable (cstack.cpp, r19).

    Quote Originally Posted by Elysia View Post
    Yes, you can do that. So long as you do not create a new variable with an identical name to one stored as a class member, you do not need "this->". It is implicitly deduced by the compiler.
    If you do shadow a member variable, then you do need "this->" to distinguish between the two variables. If you omit it, the local variable will be used instead.
    Many programmers actually tend to prefer prefixing or subfixing (is that a word?) member variables to distinguish them from local ones. I always tend to add "m_" to all member variables.
    Good tip. I've seen the m_ prefix used quite a bit in the book I'm going through, and as my mistake has shown me, there is evidently a really good reason for doing so.

    Quote Originally Posted by whiteflags View Post
    You got the plot.
    Awesome, thanks for the confirmation.
    It is better to fail with honor than win by deceit
    - unknown

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

  3. #3
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by deoren View Post
    To answer that question, no, you should not do that. When I did so (cstack.cpp, r18) I was thinking of getting rid of the variable itself, but instead I was freeing up the memory I had just purposely reserved for the updated array (and now assigned to this->plist). My best guess is that I should allow the tmp_stack pointer to pass out of scope and be removed like an ordinary local variable (cstack.cpp, r19).
    Obviously you mustn't because you've just assigned tmp_stack to plist. So if you delete tmp_stack, plist will point to invalid memory now.
    The right thing was done in (18), which deletes the old memory only and leaves the new intact.
    Once again, you don't have to worry about this if you use smart pointers. I urge you to get familiar with them.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

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