Pointer always returning NULL

Hello,

Whenever the below code goes through the pointer transition always returns NULL despite being edited in the function.

This happens 60 times a second:
1
2
3
4
5
6
      current_room->transition(transition);
      if(transition!=NULL)
      {
        current_room=transition;
        transition=NULL;
      }

Transist changes to 1 on click:
1
2
3
4
5
6
7
8
9
10
void room_intro::transition(base_room* next)
  {
    if(transist>0)
    {
      switch(transist)
      {
      case 1: next = new room_floor1_1(); break;      
      }
    }
  }

So when clicked transition should go from NULL to a new room_floor_1_1 but it always returns NULL. I can't seem to find why this is happening.
Any help would be greatly appreciated.
Thanks,

Tyler
The pointer is passed by value so the pointer inside the transition function is a copy of the pointer you pass to it. Changes made to the copy (inside the function) will not affect the original pointer.
I'm not sure how it's being passed by value when I set it to be passed by address here:
void room_intro::transition(base_room* next)
when */& is used in prototype and declarations it just means it a pointer, its not a dereference/reference operator
Sorry if that didnt make sense :p
Last edited on
The pointer itself is being passed by value.

Quick fix:
1
2
3
4
5
6
7
8
9
10
void room_intro::transition(base_room*& next)
  {
    if(transist>0)
    {
      switch(transist)
      {
      case 1: next = new room_floor1_1(); break;      
      }
    }
  }

Alternatively:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
current_room->transition(&transition);

//...

void room_intro::transition(base_room** next)
  {
    if(transist>0)
    {
      switch(transist)
      {
      case 1: *next = new room_floor1_1(); break;      
      }
    }
  }
Last edited on
Huh, I honestly didn't know it worked that way. Thanks for your help everyone.

Tyler
Topic archived. No new replies allowed.