Address of 'this'

This is a discussion on Address of 'this' within the C++ Programming forums, part of the General Programming Boards category; I have a class that looks like this: Code: class CONNECTION { public: bool ReadRequest() { DoSomething(&this); } }; The ...

  1. #1
    Banned nickname_changed's Avatar
    Join Date
    Feb 2003
    Location
    Australia
    Posts
    986

    Address of 'this'

    I have a class that looks like this:
    Code:
    class CONNECTION
    {
      public:
        bool ReadRequest()
        {
           DoSomething(&this);
        }
    };
    The DoSomething function takes a pointer to a CONNECTION class as a parameter. So, I need to tell it the address of the calling CONNECTION class. But I can't use &this, so does anyone know what I should use to do this?

  2. #2
    *******argv[] - hu? darksaidin's Avatar
    Join Date
    Jul 2003
    Posts
    314
    uhm... this is a pointer, why would you pass it's address ?
    [code]

    your code here....

    [/code]

  3. #3
    Banned nickname_changed's Avatar
    Join Date
    Feb 2003
    Location
    Australia
    Posts
    986
    Well this is what DoSomething looks like:
    Code:
    bool DoSomething(CONNECTION * Conn)
    {
       ..crap...;
    }
    My limited understanding of pointers tells me I have to set it to an address, so thats what I'm trying to do. Do I have it all wrong?

    PS: I just went to post the code above and I was told with a message box I didn't use code tags! What a great idea!

  4. #4
    *******argv[] - hu? darksaidin's Avatar
    Join Date
    Jul 2003
    Posts
    314
    I mean I'm a C++ newbie so there might be something I overlook, but your functions seems to expect a pointer so you should pass it a pointer.

    You passed it the adddress of the variable that holds the target address of this. In that case DoSomething would have to look like....

    bool DoSomething(CONNECTION **Conn).

    Try DoSomething(this) .
    [code]

    your code here....

    [/code]

  5. #5
    Registered User
    Join Date
    Oct 2002
    Posts
    291

    Re: Address of 'this'

    Originally posted by stovellp
    I have a class that looks like this:
    Code:
    class CONNECTION
    {
      public:
        bool ReadRequest()
        {
           DoSomething(&this);
        }
    };
    The DoSomething function takes a pointer to a CONNECTION class as a parameter. So, I need to tell it the address of the calling CONNECTION class. But I can't use &this, so does anyone know what I should use to do this?
    I thought 'this' was a pointer so
    Code:
    DoSomething( this );
    should work.

  6. #6
    Banned nickname_changed's Avatar
    Join Date
    Feb 2003
    Location
    Australia
    Posts
    986
    I had already tried that, this is what I got:

    Code:
    --------------------Configuration: SWSBeta - Win32 Release--------------------
    Compiling...
    connection.cpp
    C:\SWEBS\SWSBeta\connection.cpp(188) : error C2664: 'bool (class std::basic_istringstream<char,struct std::char_traits<char>,class std::allocator<char> > &,class CONNECTION *)' : cannot convert parameter 1 from 'class std::basic_istringstream<char,s
    truct std::char_traits<char>,class std::allocator<char> > *' to 'class std::basic_istringstream<char,struct std::char_traits<char>,class std::allocator<char> > &'
            A reference that is not to 'const' cannot be bound to a non-lvalue
    Error executing cl.exe.
    
    SWSBeta.exe - 1 error(s), 0 warning(s)

  7. #7
    Registered User filler_bunny's Avatar
    Join Date
    Feb 2003
    Posts
    87
    *this is a const pointer and therefore cannot be changed. You should declare the recieving pointers to be of type const.
    Visual C++ .net
    Windows XP profesional

  8. #8
    Registered User filler_bunny's Avatar
    Join Date
    Feb 2003
    Posts
    87
    Code:
    #include <iostream>
    
    using namespace std;
    
    // The Foo Class
    //
    class Foo
    {
    public:
    	const Foo & getThis(void) const;
    	void doSomething(void) const;
    };
    
    const Foo & Foo::getThis(void)const 
    {
    	return *this;
    }
    
    void Foo::doSomething(void) const
    {
    	cout << "Here!" << endl;
    }
    
    // The Bar Class
    //
    class Bar
    {
    public:
    	void gotThis(const Foo & f) const;
    };
    
    void Bar::gotThis(const Foo & f) const
    {
    	f.doSomething();
    }
    
    int main(void)
    {
    	Foo test1;
    	Bar test2;
    	
    
    	test2.gotThis(test1.getThis());
    
    	cin.get();
    
    	return 0;
    }
    Just to clarify, the "this" pointer is defined as being a const pointer to an object. This means essentially that you also have an object (the object pointed to by this) which is constant. And you may only call constant methods of a constant object, those methods that promise not to change the contents of the object.

    I have to admit, this example is a little round about and I am not a genius with pointers or references, but I hope this clarifies it a little.

    Also, notice that I use references and not pointers.
    Last edited by filler_bunny; 09-01-2003 at 08:35 AM.
    Visual C++ .net
    Windows XP profesional

  9. #9
    Banned nickname_changed's Avatar
    Join Date
    Feb 2003
    Location
    Australia
    Posts
    986
    Sorry to have wasted everyones time here guys, you were all right, it should have worked as 'this' and nothing else. The error I was getting was actually for another argument sent to the function (the code I showed you was a very dumbed down version). Sorry, and thanks for all the help!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. What does this do (Windows API)?
    By EVOEx in forum Windows Programming
    Replies: 4
    Last Post: 12-19-2008, 10:48 AM
  2. Writing array, to file
    By zootreeves in forum C Programming
    Replies: 9
    Last Post: 09-08-2007, 06:06 PM
  3. I thought pointers were pointers...
    By keira in forum C Programming
    Replies: 19
    Last Post: 08-16-2007, 12:48 AM
  4. DX - CreateDevice - D3DERR_INVALIDCALL
    By Tonto in forum Game Programming
    Replies: 3
    Last Post: 12-01-2006, 07:17 PM
  5. pointers
    By InvariantLoop in forum C Programming
    Replies: 13
    Last Post: 02-04-2005, 09:32 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21