How to create an object only in some specific case ?

I'm programming a main funcion that solves a mechanical problem using several other functions. Depending on the kind of mechanical problem considered (defined by the user), I need or don't need to create a specific object.

My first (naive) idea was to declare and initialize my object in a switch statement and call the functions that use it in other switch statements. However (as I should have expected it) the object becomes a local variable in the first switch statement and I can't use it in the next switch statement...

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
// header
int main(int argc, char * argv[]) {

  // problem initialization
  int const pb_type = 1;

  // some code

  switch (pb_type) {
    case 1:
      MyObject object(params);
  }

  // some code

  switch (pb_type) {
    case 1:
      function(ojbect);
  }

  // some code
}


I know that I could declare a pointer to my object before the first switch statement and initialize my object in the switch statement so that it becomes a variable of my main function. But I would declare a pointer that may never be used... So it does not feel like the good solution.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
// header
int main(int argc, char * argv[]) {

  // problem initialization
  int const pb_type = 1;

  // some code

  MyObject * object;
  switch (pb_type) {
    case 1:
      object = new MyObject(params);
  }

  // some code

  switch (pb_type) {
    case 1:
      function(*object);
  }

  // some code
}


However the only other solution I see is to extend the switch statement so that it includes all the part of my main function where I need to use this object (or to create an intermediate function to do things more properly). But that means duplicating almost all my main function, so it does not feel like the good solution has well...

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
// header
int main(int argc, char * argv[]) {

  // problem initialization
  int const pb_type = 1;

  switch (pb_type) {
    case 1: {
      // some code
    
      MyObject object(params);
 
      // some code

      function(ojbect);

      // some code
      
      break;
    }

    case 2: {
      // some code
    }
  }
}      


So does anybody have any idea of an alternative solution ?
I think your idea with the pointer sounds like the best way to do it.
Why cannot you create object right before you call function:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
int main(int argc, char * argv[]) {
  // problem initialization
  int const pb_type = 1;

  // some code

  switch (pb_type) {
    //There is no case 1 here.
  }
  // some code

  switch (pb_type) {
    case 1:
      //A temporary passed into function. You can replace with named object if you need.
      function(MyObject(params));
  
  // some code
}
Thank you for your answers Peter87 and MiiNiPaa.

To answer your question MiiNiPaa, I think your solution would work but it is not the best in my case. I use this object several times in my main function which consists of the problem initialization plus some code (where I declare this object) and of a "do... while..." loop that solves the problem iteratively (where I use this object). So I would be creating the same temporary object several hundred times. But in a simpler case it would be a good solution !

I guess that Peter87 is right and that the solution with the pointer is the best choice in my case...
Another aproach to reduce code duplication is extract common code in a separate functions.
That way you would not have duplicating code in main (and functions longer than 50 lines are bad to have anyway):

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
int main(int argc, char * argv[]) {

  // problem initialization
  int const pb_type = 1;

  switch (pb_type) {
    case 1: {
      func1(/*...*/);    
      MyObject object(params);
      func2(/*...*/);
      function(ojbect);
      func3(/*...*/);
      break;
    }
    case 2: {
      // some code
    }
  }
}    
Maybe move around code in main, separate common parts in meaningful function, reduce dependencies... This is the process called refactoring, which is used to improve code, making it easier to understand and less error-prone.
Well it's already how my code is made but it can certainly be improved. I will try to look in this direction, and come back if I have more specific question... Thanks for your help.
Topic archived. No new replies allowed.