-
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?
-
uhm... this is a pointer, why would you pass it's address ?
-
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!
-
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) .
-
Re: Address of 'this'
Quote:
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.
-
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)
-
*this is a const pointer and therefore cannot be changed. You should declare the recieving pointers to be of type const.
-
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.
-
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!