Thread: about "this"

  1. #1
    Registered User
    Join Date
    Oct 2004
    Posts
    30

    about "this"

    i am writing a function in which based on some condition i am supposed to call another function with this passed as a parameter to it..

    my code is somewhat like this..
    Code:
    class b
    {
    	public:
    	b& func1(b &,b &);
    	b& func2(b &);
    };
    
    b& b :: func1(b &x,b &y)
    {
    	z = new b();
    	.
    	.
    	return z;
    }
    
    b& b :: func2(b &x)
    {
    	z = new b();
    	if(condition)
    	{
    		//if i use this --no matching function call to 'b::func1(b* const,b&)'
    		z = func1(this,x);
    		//if i use this --cannot convert 'b' to 'b*' in assignment
    		z = func1(*this,x);
    	}
    	else
    	{
    		z = x;
    	}
    	return z;
    }
    please help me...
    Syra
    Amateur's urge to master C/C++

  2. #2
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    Second one is correct, first one is not.
    Your problem is with z. Is it a pointer or a reference/actual object. You treat it as a pointer once ('z = new b()'), and as an object or reference the second time ('z = func1...' and 'z = x' where x is a reference). If it is a pointer, then your memory management is a bit flawed as you lose the pointer to the data you allocate with new (memory leak).
    The word rap as it applies to music is the result of a peculiar phonological rule which has stripped the word of its initial voiceless velar stop.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. another way for "this" pointer
    By dukebdx12 in forum C++ Programming
    Replies: 12
    Last Post: 06-23-2008, 10:52 PM
  2. Passing "this" as function parameter
    By pgavigan in forum C++ Programming
    Replies: 15
    Last Post: 07-13-2007, 10:06 AM
  3. quick question concerning "this"
    By HIM in forum C++ Programming
    Replies: 4
    Last Post: 11-04-2006, 07:17 PM
  4. the "this" pointer
    By faze in forum C++ Programming
    Replies: 7
    Last Post: 06-24-2005, 01:20 AM
  5. Replies: 2
    Last Post: 10-13-2001, 10:22 PM