Passing "this" pointer to member object

Started by
26 comments, last by rip-off 12 years, 10 months ago
I'm currently revamping the structure for my game, and I need to pass the address of an object to one of its members. I tried to use the "this" pointer, but that didn't work. I was able to get around the problem by passing the pointer of the object to its own constructor and then passing that to the member. I'm guessing there is a better way to do this though.

Thanks!
Scott Wilkewitz
Advertisement
1, Why do you want to pass the address?
2, Why passing "this" doesn't work? What error?

More details please.

https://www.kbasm.com -- My personal website

https://github.com/wqking/eventpp  eventpp -- C++ library for event dispatcher and callback list

https://github.com/cpgf/cpgf  cpgf library -- free C++ open source library for reflection, serialization, script binding, callbacks, and meta data for OpenGL Box2D, SFML and Irrlicht.


1, Why do you want to pass the address?
2, Why passing "this" doesn't work? What error?

More details please.


1, I've got a chain of classes that store each other, and I want to be able to access methods from higher-up classes.

2, Its a runtime error. Basically, when I use the pointer to access the methods nothing happens. Perhaps the problem is with parallel objects? All I know is that getting the pointer from outside the constructor works.
I don't quite understand what exactly is your goal. As it sound like, you simply want something like this, but it's rather useless:

class A
{
public:
A() : memberPointer(this) {}
~A() {};

private:
A* memberPointer;
};


Or maybe instead of a pointer as the member variable, you have another class B and you need to pass A pointer to B through the constructor or a method or whatever. Shouldn't be a problem nonetheless.
It's quite possible to do this:
class Member
{
public:
SetOwner(Owner *owner);
};

class Owner
{
public:
Owner() { member.SetOwner(this);}

Member member;
}


This is wrong:

Owner owner;
owner.member.SetOwner(this);

But you can go like this:

Owner owner;
owner.member.SetOwner(owner);


If you are wanting a reference (or just a regular variable) and not a pointer, you can go like this:

class Member
{
public:
SetOwner(Owner &owner);
};

class Owner
{
public:
Owner() { member.SetOwner( *this ); } //De-reference the pointer.

Member member;
}
Duplicate threads merged.

Wielder of the Sacred Wands
[Work - ArenaNet] [Epoch Language] [Scribblings]

If you pass the pointer during construction, you shouldn't call any methods on that pointer until the enclosing class has completed construction. Otherwise you'll be accessing partially constructed object. In particular, calling virtual methods when only part of the inheritance hierarchy has been constructed is a bad idea.

See if you can separate the objects to introduce a third object that both depend on:

class Shared
{
public:
Shared(int n) : n(n)
{
}

void foo()
{
std::cout << "Shared: " << n << '\n';
}

private:
int n;
};

class Member
{
public:
Member(Shared *shared)
{
shared->foo();
}
};

class Owner
{
public:
Owner()
:
shared(42),
member(&shared)
{
}

private:
Shared shared; // <-- Ordering here is vital!
Member member;
};

int main()
{
Owner owner;
}

This is cleaner if you can manage it. If not, you probably should wait until the Owner is constructed before giving the Member a pointer to it. Unless you can guarantee you won't be doing anything dangerous with that pointer until the Owner is fully constructed.
I am using the pointers to create a hierarchy of unrelated classes. The pointers would create a sort of "spine."

Game
>World
>Actor

A Game would hold an instance of World, and a World would hold a pointer of Game. So when Game is being constructed I want to create a World and set the World's pointer to the Game.

I thought that using the "this" pointer would work fine, but I wasn't able to get the "spine" working.



"This is wrong:"

[color="#660066"]Owner owner[color="#666600"];
owner[color="#666600"].member[color="#666600"].[color="#660066"]SetOwner[color="#666600"]([color="#000088"]this[color="#666600"]);



"But you can go like this:"

[color="#660066"]Owner owner[color="#666600"];
owner[color="#666600"].member[color="#666600"].[color="#660066"]SetOwner[color="#666600"](owner[color="#666600"]);




I know the first one is wrong, I never tried to do that. The second way is how I've been getting around the problem... though I've been passing &owner instead of owner.



I am using the pointers to create a hierarchy of unrelated classes. The pointers would create a sort of "spine."

Game
>World
>Actor

A Game would hold an instance of World, and a World would hold a pointer of Game. So when Game is being constructed I want to create a World and set the World's pointer to the Game.

I thought that using the "this" pointer would work fine, but I wasn't able to get the "spine" working.
... gd.net reformatted puke elided ...

I know the first one is wrong, I never tried to do that. The second way is how I've been getting around the problem... though I've been passing &owner instead of owner.

So, what's the problem? That's fairly idiomatic.

World::World()
: game(this)
{ }

Game::Game(World* world)
: actor(world)
{ }

Stephen M. Webb
Professional Free Software Developer

This topic is closed to new replies.

Advertisement