0

While I am learning C++ based on how develop in modern PHP frameworks I decided to use the Dependency Injection pattern with services as Singletons.

For making myself clear let suppose we have the collowing classes:

class B {
  public:
    B(){};
}

class A{
  public:
    // Dependency Injecting class B into class A
    A(B b):b(b){}
  private:
   const B b;
}

So in this example I wondered if I will gain better performance if I used pointer instead of an Object. In other words would it be better if class A had been:

class A{
  public:
    // Dependency Injecting class B into class A
    A(B* b):b(b){}
  private:
   const B *b;
}

Also, what is the downside in the pointer-based approach?

2
  • I would love the long answer as well. Jun 3, 2019 at 17:15
  • 1
    "const B* b;" Not related, but I think you should really use spaces according to grammar: const B *b; because grouping is const B; and *b
    – curiousguy
    Jun 4, 2019 at 18:47

2 Answers 2

4

So in this example I wondered if I will gain better performance if I used pointer instead of an Object.

No.

I will be very surprised if that is the case.

Also, what is the downside in the pointer-based approach?

You'll have to define ownership policy of the object that the pointer points to. If the client is expected to retain ownership of the object,

  1. The client of A has to make sure that the pointer is valid as long as the A object is alive.

  2. Behavior of A will be unpredictable since it depends on the client to make sure that the pointer stays valid.

If A is expected to take over ownership of the object,

  1. You'll have to make sure the object that the pointer points to is deleted before the A object is destructed.

  2. You'll have to make sure to follow The Rule of Three so that copy construction and copy assignment are dealt with correctly.

4
  • Would the use of smart pointers change anything here? I can see how the use of normal pointers would be a nightmare solution. Jun 3, 2019 at 18:36
  • @BerinLoritsch, it would help with the issues related to The Rule of Three but you still have to be clear on the ownership policy. That would dictate whether you use shared_ptr or unique_ptr.
    – R Sahu
    Jun 3, 2019 at 18:45
  • Thank you. In the realm of C# and Java, there is a container that initializes all of the objects in use in the system. It's responsibility is to ensure the order of initialization is done such that the leaves of the DAG are built first. The container is also responsible for tearing down all of the objects. Granted that doesn't work the same verbatim in C++. Jun 3, 2019 at 19:28
  • @BerinLoritsch, I have some experience in C# and no experience in Java. I can't compare between what C# and Java do vs. what C++ does.
    – R Sahu
    Jun 3, 2019 at 19:37
4

You also need to ask the reverse: what is the downside to using an object directly?

The answer is that the object version captures a B b and will not permit an arbitrary subclass of B to be injected — unless it offers a lossy conversion to B.

If B alone has sufficient customization for your scenario, maybe that's fine.  But the pointer version will directly accept subclasses of B as well as plain Bs.

Since the purpose is injection, I would presume that accepting subclasses of B might sometimes be of interest here.  YMMV.

2
  • So for DI a pointer version is preferred right? Usually, it is common to have a Superclass or in other Languages an Interface. In C++ case because no intefaces are offered hence a SuperClass may be required therefore a pointer is a way to go. Right? Jun 3, 2019 at 19:34
  • 2
    Yes, that's often the case. (You might use smart pointers to establish the ownership as @RSahu is getting at.)
    – Erik Eidt
    Jun 3, 2019 at 19:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.