Skip to main content
GameDev.net gamedev.net
🔒 Locked

C++ - passing "this" as pointer?

Started by BenB Apr 19, 2008 at 6:58 AM 8 replies 17.8k views
Original Post
BenB
BenB
I have written some kind of an interface "window" class. ** Never mind, please read my reply for my question ** I'm writing this from a cell, sorry if it is not written too good or if this is searchable... Thanks [Edited by - BenB on April 21, 2008 8:33:56 AM]
Porthos
Porthos
This passes a copy of the pointer, not a copy of the instance.

To pass a copy of the instance:

void setfocus (<class_name> object);// ----setfocus(*this);


Passing &this passes a Pointer to a pointer, which causes a compiler error.

Best regards,
Porthos
ToohrVyk
ToohrVyk
Quote:
Original post by BenB
however this seems to pass only a copy of the instance, not a pointer to it.


What do you mean?
Molle85
Molle85
'this' is a pointer to the current object...

so what you want is to do.

void setfocus( window* ptr )
{
// DoStuff
}

setfocus( this );

BenB
BenB
Hi, thanks for the help, however I didn't make myself clear at all.

Actually, what I want is the address to "this".

I've tried to do something like this.

cWindow *windowInFocus;someprogramfunction(){  newWindow = cWindow();}cWindow::cWindow(){  setFocus(this);}cWindow::setFocus(cWindow window){  windowInFocus = window;}


So that I could then use the pointer windowInFocus like this, for example

windowInFocus->posX = 40;

and it would change the original posX of newWindow.

However, by using windowInFocus I'm not changing the actual newWindow that I want to change.
That's because I have passed a pointer of the newWindow to setFocus, and not its address.
So I thought the solution should be changing the line to

setFocus(&this);


However that returns an error of:
error C2102: '&' requires l-value

How can I come around this?
Thanks very much!
agm_ultimatex
agm_ultimatex
You should be able to set a pointer of type cWindow to this.

windowInFocus = this;

Also with your code, im not sure if: newWindow = cWindow(); is correct. Should you not have it as: cWindow newWindow; or cWindow newWindow = new cWindow();
Sc4Freak
Sc4Freak
"this" is a pointer - meaning that you use it to access and modify the contents of the object that it's pointing to.

The address of "this" is useless - it would allow you to change the value of the this pointer. Which makes even less sense.

You're confusing your layers of indirection. "this" is not the actual object, it is a pointer to the object. If you give another function the this pointer, that function can use that pointer to modify the original object. It works like any other pointer.
i like sublime
i like sublime
Quote:
Original post by BenB
Actually, what I want is the address to "this".


"this" is an address; and a pointer-to-a-pointer is generally unnecessary for most tasks similar to what's going on here...

Quote:
Original post by BenB
I've tried to do something like this.

cWindow *windowInFocus;someprogramfunction(){  newWindow = cWindow();}cWindow::cWindow(){  setFocus(this);}cWindow::setFocus(cWindow window){  windowInFocus = window;}



Is that supposed to be " ::setFocus(cWindow* window) "? As it is, it's taking a value, not an address as an argument.

Quote:
Original post by BenB
So I thought the solution should be changing the line to

setFocus(&this);



That would be a pointer-to-a-pointer... :-)

I figure it's your method argument syntax that's throwing you. Haven't personally been near a compiler in a couple months though...
rip-off
rip-off
Of course, idiomatic C++ would use a reference, not a pointer.

I don't understand why you are passing any argument at all though. Member functions already have a "this" pointer, which is implicitly passed using pixie magic [smile] (or close enough).

Window::Window(){   grabFocus();}void Window::grabFocus(){    windowInFocus = this;}


It is possible to design the system in such a way that the Window class shouldn't be responsible for remember what is in focus. For example, what happens when the "windowInFocus" is destroyed? I assume your code handles this gracefully, correctly losing the focus on that window, but who gains it?

You could have a "focus stack" somewhere that remembers all the previous Window instances that have had focus in the past, in the correct order.
theonecalledtom
theonecalledtom
Try changing setFocus to this:

cWindow::setFocus(cWindow &window)
{
windowInFocus = &window
}

then call

setFocus(*this); //This does not create a copy because the compiler knows the function expects a reference

Without the &s you are passing by value - i.e. when calling the function the program will create a new cWindow object. References are exactly like pointers except they declare two useful things about your function:
1) It expects the reference to be valid (pointers can be NULL, references *shouldn't* be). In other words any error checking should be done at a higher level.
2) The function only expects a single item.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.