Jump to content
  • Advertisement
Sign in to follow this  
swilkewitz

Passing "this" pointer to member object

This topic is 2599 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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

Share this post


Link to post
Share on other sites
Advertisement
1, Why do you want to pass the address?
2, Why passing "this" doesn't work? What error?

More details please.

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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;
}

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.


Share this post


Link to post
Share on other sites

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)
{ }

Share this post


Link to post
Share on other sites

[quote name='swilkewitz' timestamp='1306940984' post='4818293']
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)
{ }

[/quote]

That is what I tried to do originally, but when I called higher-up functions using the pointers nothing happened. By using the workaround the pointers work, but I'd rather not use the workaround because that doesn't actually solve the problem.

Also, I'm getting memory problems now. I've been experimenting a lot....
When I start my game in debug mode everything is fine, but when I switch to release mode eveything goes haywire. I looked at taskmanager while running in release and I noticed the used memory cycling between 1.5 GB and 2.5 GB before crashing.

Unhandled exception at 0x768bb727 in Triax Bridge Command.exe: Microsoft C++ exception: std::bad_alloc at memory location 0x0035ed40..

I'm guessing I'm not being very careful with my pointers.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!