Thread: isit ok using *this pointer

  1. #1
    Registered User
    Join Date
    May 2009
    Posts
    84

    isit ok using *this pointer

    Code:
    void GetItem()
    		{
    			for (int iii=0; iii!=MAX_INV; iii++)
    				if (charInv[iii].id==0){ charInv[iii] = *this); break;}
    				else if (charInv[iii].id==id)
    					if (amount < stackSize){ amount++; break;}}
    is it ok to use *this in that case?

  2. #2
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    It will make a copy of the current object, so sure.
    You could also store a pointer to the object, but then you must beware of dangling pointer issues if the object goes out of scope.
    Your indentation needs work, too, if I may say so (too many spaces/tabs for indentation).
    Especially, you should put the ending } on a separate line. It makes it much more readable.
    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.

  3. #3
    Registered User
    Join Date
    May 2009
    Posts
    84
    Quote Originally Posted by Elysia View Post
    It will make a copy of the current object, so sure.
    You could also store a pointer to the object, but then you must beware of dangling pointer issues if the object goes out of scope.
    Your indentation needs work, too, if I may say so (too many spaces/tabs for indentation).
    Especially, you should put the ending } on a separate line. It makes it much more readable.
    Hehe, thanks.

  4. #4
    Registered User
    Join Date
    May 2009
    Posts
    84
    Quote Originally Posted by Elysia View Post
    It will make a copy of the current object, so sure.
    You could also store a pointer to the object, but then you must beware of dangling pointer issues if the object goes out of scope.
    Your indentation needs work, too, if I may say so (too many spaces/tabs for indentation).
    Especially, you should put the ending } on a separate line. It makes it much more readable.
    Code:
    		void AddItem(int A)
    		{
    			int B;
    
                amount += A;
    
    			if( amount < stackSize )
    			{
    				B = amount - stackSize;
    
    				GetItem(B)
    			}
    		}
    
            void GetItem(int A)
    		{
    			for( int iii=0; iii != MAX_INV; iii++ )
    
    				if( charInv[iii].id == 0 )
    				{ 
    					charInv[iii] = *this; 
    					charInv[iii].additem(A); 
    					break;
    				}
    
    				else if( charInv[iii].id == id )
    					if( charInv[iii].amount < stackSize )
    					{ 
    						charInv[iii].AddItem(A); 
    						break;
    					}
    		}
    say, does it make any sense?

  5. #5
    Registered User
    Join Date
    Apr 2006
    Posts
    2,149
    You mix tabs and spaces. Don't.

    Besides that one would expect a method called GetItem, to fetch and return an item, not add A to elements of some array called charInv.

    Also C++ is case sensative so AddItem and additem are two different methods, or broken code if the latter method doesn't exist.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Following CTools
    By EstateMatt in forum C Programming
    Replies: 5
    Last Post: 06-26-2008, 10:10 AM
  2. Quick Pointer Question
    By gwarf420 in forum C Programming
    Replies: 15
    Last Post: 06-01-2008, 03:47 PM
  3. Parameter passing with pointer to pointer
    By notsure in forum C++ Programming
    Replies: 15
    Last Post: 08-12-2006, 07:12 AM
  4. Direct3D problem
    By cboard_member in forum Game Programming
    Replies: 10
    Last Post: 04-09-2006, 03:36 AM
  5. Struct *** initialization
    By Saravanan in forum C Programming
    Replies: 20
    Last Post: 10-09-2003, 12:04 PM