vendredi 9 octobre 2020

What's the preferred way to instantiate one class in another?

Pretty basic question. Has to do with design preference but I think there are some caveats that I'm missing.

I want to have an instance of some class inside another class. The way I see it, I have 3 options:

  1. Have the object class2 inside class1 as I have it below. This means it gets constructed when an instance of class1 gets constructed. My issue here is that it may need a default and/or copy constructor? When I instantiate a class1 object below, does it construct class2_inst(args) using that constructor? Or is class2_inst already created with some default constructor and the line class2_inst(args) simply copies an anonymous class2 object?

    class class2 {
      public class2(args) {
        ...
      }
    }
    
    class class1 {
      protected class2 class2_inst;
      public class1(args) : class2_inst(args) {
        ...
      }
    }
    
  2. I can have a pointer to class2:

    class class2 {
      public class2(args) {
        ...
      }
    }
    
    class class1 {
       protected class2* class2_inst;
       public class1(args) {
        class2_inst = new class2(args);
        ...
       }
    }
    

    This has the advantage that class2 isn't instantiated until I explicitly call the new operator.

  3. Then there's using a reference instead of a pointer:

    class class2 {
      public class2(args) {
        ...
      }
    }
    
    class class1 {
      protected class2& class2_inst;
      public class1(args) {
        class2_inst = new class2(args);
        ...
      }
    }
    

I want class2_inst's life-time to match that of class1. No funny business with having class2_inst living outside of class1. So references may be the way to go.

Of the 3 methods, which is everyone's preferred way, and why?

Aucun commentaire:

Enregistrer un commentaire