简体   繁体   English

使用A类类型的shared_ptr作为B类的成员变量

[英]using shared_ptr of a type of Class A as a member variable of class B

Assume that my class B is something like this:假设我的 B 类是这样的:

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

The Constructor from class A looks like: A 类的构造函数如下所示:

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.现在,我想为我的类 B 编写构造函数,其中使用上面编写的构造函数构造(初始化或分配)类 A 的对象。 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" ...我尝试了类似下面的方法,但我得到了诸如“术语不评估为采用 1 个参数的函数”和“在这种情况下初始化列表是意外的”之类的错误......

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?一般来说,我们为什么要使用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;其实我不明白如果private: std::shared_ptr < class A > sp; i already created the object by calling the default constructor of class A?我已经通过调用 A 类的默认构造函数创建了对象? So by make_shared i am creating another object?所以通过 make_shared 我正在创建另一个对象?
  3. How can i use the constructor initilizer list using : ?我如何使用构造函数初始化器列表:? PS According to one answer below, it seems that i cannot use the initializer list approach. PS根据下面的一个答案,似乎我不能使用初始化列表方法。 Because the simple computation i wrote for a1 and b2 were only for showing my problem.因为我为 a1 和 b2 编写的简单计算只是为了显示我的问题。 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)} .我的实际计算比这更长,我认为我不能使用像B (double d,double e): sp{std::make_shared<A>(d*2, e*2)}这样的方法。

Thanks谢谢

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):如果你想初始化sp你可以在如下所示的构造函数初始化列表中完成它(而不是像你做的那样在构造函数的主体内):

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

Fromstd::make_shared's documentation :来自std::make_shared 的文档

 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 . std::make_shared用于构造T类型的对象并将其包装在std::shared_ptr中,使用args作为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.在这种情况下,模板参数TA ,因此std::make_shared用于构造std::shared_ptr<A>并在构造函数初始化列表中用它初始化sp

Note that we can also use in-class initializer to initialize sp .请注意,我们还可以使用类内初始化程序来初始化sp

From How to create and use shared_ptr instance :如何创建和使用shared_ptr实例

Whenever possible, use the make_shared function to create a shared_ptr when the memory resource is created for the first time.首次创建内存资源时,尽可能使用make_shared函数创建shared_ptr make_shared is exception-safe . make_shared异常安全的 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.如果不使用make_shared ,则必须使用显式new表达式来创建对象,然后再将其传递给shared_ptr构造函数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM