Object Keeps Falling out of scope. HELP!

I'm clueless as to why creating an instance of an object while in a conditional statement would "go out of scope" once you leave that conditional statement. He's the snippet of my code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
else if(command == 6 || command == 7 || command == 8)
{
  if(command == 6) //when I remove this conditional, the line object doesn't "fall out of scope" anymore
  {
  out(temp_array, size, X0, Y0, X1, Y1); //returns values for X0, Y0, X1, Y1
  line *object = new line(X0, Y0, X1, Y1);
  }
  
  while(command_stack.top() != BOTTOM)
  {
  if(command_stack.top() == TRANSLATE)
  {
    command_stack.pop();
    yT = command_stack.top();
    command_stack.pop();
    xT = command_stack.top();
    command_stack.pop();
    object->translate(xT, yT); //the object keeps falling out of scope?!
   }
  }


and if it helps, here's my class definitions:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
class line : public point //point class contains virtual function translate
{
public:
  line(double X0, double Y0, double X1, double Y1);
  line(void);
  virtual void translate(double xT, double yT); 
private:
  double X0;
  double Y0;
  double X1;
  double Y1;
};

line::line(double X0, double Y0, double X1, double Y1) : X0(X0), Y0(Y0), X1(X1), Y1(Y1)
{/*Body intentionally left empty*/}

line::line(void) : X0(0), Y0(0), X1(0), Y1(0)
{/*Body intentionally left empty*/}

void translate(double xT, double yT)
{
  X0 += xT;
  Y0 += yT;
  X1 += xT;
  Y1 += yT;
}


Any help is much appreciated. Thank you in advance.
That's what a scope is..
To solve this, declare object before the if. Note though that if command is not 6, you'll get a segfault..
Oh, I see. I would declare it outside the if, but I have to declare other types of objects depending on what the command is. Is there a way to make the whole class static?
look into polymorphism.
Topic archived. No new replies allowed.