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) };
- I'd appreciate to correct me either if i did typing mistake or if i misunderstood something conceptually(or any other explanation)
- 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? - 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