[英]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) };
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 我正在创建另一个对象?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.在这种情况下,模板参数T
是A
,因此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 ashared_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 usemake_shared
, then you have to use an explicitnew
expression to create the object before you pass it to theshared_ptr
constructor.如果不使用make_shared
,则必须使用显式new
表达式来创建对象,然后再将其传递给shared_ptr
构造函数。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.