Home > Back-end >  using shared_ptr of a type of Class A as a member variable of class B
using shared_ptr of a type of Class A as a member variable of class B

Time:05-16

Assume that my class B is something like this:

class B {
B (double d,double e)
private:
std::shared_ptr < class A > sp;
}

The Constructor from class A looks like:

A(double a, double b){...};

Now, I want to write the constructor function for my class B, in which an object from class A is constructed(initialized or assigned) using the constructor function written above. Can someone explain me how can i do it professionally? I tried something like below but i get errors like "term does not evaluate to a function taking 1 argument" and "an initializer list is unexpected in this context" ...

class B (double d, double e){
double a1 = d*2;
double b2=  e*2;
sp { std::make_shared<class A>(a1,b2) };
  1. I'd appreciate to correct me either if i did typing mistake or if i misunderstood something conceptually(or any other explanation)
  2. In general, why should we use make_shared? what are the other approaches and what are their pros and cons? Actually i don't understand if by private: std::shared_ptr < class A > sp; i already created the object by calling the default constructor of class A? So by make_shared i am creating another object?
  3. How can i use the constructor initilizer list using : ? P.S According to one answer below, it seems that i cannot use the initializer list approach. Because the simple computation i wrote for a1 and b2 were only for showing my problem. My real computations are longer than that and I think i cannot use approaches like B (double d,double e): sp{std::make_shared<A>(d*2, e*2)} .

Thanks

CodePudding user response:

How can i use the constructor initilizer list using :

If you want to initialize sp you can do it in the constructor initializer list as shown below, (and not inside the body of the constructor as you were doing):

//---------------------vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv-->constructor initializer list
B (double d,double e): sp{std::make_shared<A>(d*2, e*2)}
  {
      
  }

From std::make_shared's documentation:

template< class T, class... Args > shared_ptr<T> make_shared( Args&&... args );

std::make_shared is used to construct an object of type T and wraps it in a std::shared_ptr using args as the parameter list for the constructor of T.

(end quote)


The template parameter T in this case is A and thus std::make_shared is used to construct a std::shared_ptr<A> and initialize sp with it in the constructor initializer list.

Note that we can also use in-class initializer to initialize sp.

From How to create and use shared_ptr instance:

Whenever possible, use the make_shared function to create a shared_ptr when the memory resource is created for the first time. make_shared is exception-safe. It uses the same call to allocate the memory for the control block and the resource, which reduces the construction overhead. If you don't use make_shared, then you have to use an explicit new expression to create the object before you pass it to the shared_ptr constructor.

  • Related