简体   繁体   English

构造函数中的智能指针

[英]smart pointer in constructor

Story is: I have to make a class for my programming class and my custom project (Space Marine - yeah, I'm a bit of a fan) is in progress. 故事是:我必须为我的编程课和我的自定义项目(Space Marine - 是的,我是一个粉丝)进行课程正在进行中。 But I found a problem I just can't simply solve. 但我发现了一个我不能简单解决的问题。 Here are some codes that I think are important: 以下是一些我认为重要的代码:

HEADER HEADER

class SpaceMarine
{

public:
...
    SpaceMarine(); // default constructor
    SpaceMarine(std::string name, 
                unsigned int rang, 
                Statystyki& stats, 
                std::auto_ptr<Pancerz> armor, Bron& weapon);

private:

    std::string name_;
    unsigned int ranga_;
    Statystyki* stats_;
    std::auto_ptr<Pancerz> armor_;
    Bron* weapon_;
    Experience experience_;

just to make it clear : "Statystyki", "Pancerz" and "Bron" are classes that are members of main class SpaceMarine. 为了说清楚:“Statystyki”,“Pancerz”和“Bron”是主要类SpaceMarine的成员。 Project requirements: I have to make one smart pointer (here as auto_ptr). 项目要求:我必须制作一个智能指针(此处为auto_ptr)。

.CPP .CPP

SpaceMarine::SpaceMarine()
{
    name_ = "John";
    stats_ = new Statystyki();
    weapon_ = new Bron(); 
    std::auto_ptr<Pancerz> armor_(new Pancerz());


   ranga_ = 0;
}
SpaceMarine::SpaceMarine(std::string name, 
                         unsigned int rang, 
                         Statystyki& stats, 
                         std::auto_ptr<Pancerz> armor, Bron& weapon) 
: armor_(std::move(armor))
{
    name_ = name;
    ranga_ = rang;
    stats_ = stats;
    armor_ = std::move(armor);
    weapon_ = weapon;
}

Now, where the problem begins: This is the part of "main.cpp" file: 现在,问题开始的地方:这是“main.cpp”文件的一部分:

SpaceMarine SM1;
SpaceMarine SM2("Azrael", 3, S2, **P2** , B2);

// S - Stats, P - armor, B-weapon class // S - Stats,P - armor,B-weapon class

There is problem with this little thingy, called P2, which should be an auto_ptr to armor. 这个小东西有问题,称为P2,它应该是一个装甲的auto_ptr。 I have armor object P2 declared previously. 我先前已经宣布了盔甲对象P2。 I have problem "merging" auto_ptr into my constructor. 我有问题“将”auto_ptr“合并”到我的构造函数中。 Any ideas? 有任何想法吗?

Also, all advices about improving my code are welcome :) 此外,欢迎所有关于改进我的代码的建议:)

Matt 马特

Ps. PS。 My first post here! 我的第一篇帖子! :D Go easy on me ^^ :D对我很轻松^^

EDIT 编辑

Thanks to user1158692 for tidying my code Thanks to Hansmaad and user1158692 I wish I could make both your answers right for this probleme, as both helped me to deal with it ;) 感谢user1158692整理我的代码感谢Hansmaad和user1158692我希望我能为这个问题做出正确答案,因为它们都帮助我处理它;)

Here's the final code for future refrence: 这是未来参考的最终代码:

HEADER HEADER

SpaceMarine(std::string name, 
            unsigned int rang, 
            Statystyki& stats, 
            Pancerz& armor, 
            Bron& weapon);

.CPP .CPP

SpaceMarine(std::string name, 
            unsigned int rang, 
            Statystyki& stats, 
            Pancerz& armor, 
            Bron& weapon);

{
    name_ = name;
    ranga_ = rang;
    stats_ = &stats;
    std::unique_ptr<Pancerz> armor_(&armor);
    weapon_ = &weapon;
} 

// and example of declaring it in main.cpp //以及在main.cpp中声明它的示例

Bron B2("Chainsword" , 0, 6);

Pancerz P2("Power armor", 12);

Statystyki S2(6,6,4,8,20);

SpaceMarine SM2("Azrael", 3, S2, P2 , B2);

You can just pass a auto_ptr to your constructor. 您只需将auto_ptr传递给构造函数即可。 It will transfer ownership to the copy. 它会将所有权转移到副本。 However, std::auto_ptr is deprecated and should be replaced by std::unique_ptr . 但是,不推荐使用std::auto_ptr ,应该用std::unique_ptr替换它。

struct A {};
struct B 
{
    std::auto_ptr<A> a;
    B(std::auto_ptr<A> a) : a(a)
    {      
    }
};

struct C
{
    std::unique_ptr<A> a;
    C(std::unique_ptr<A> a) : a(std::move(a))
    {
    }
};

int main()
{
    std::auto_ptr<A> a{ new A };
    B b{ a };

    C c{ std::make_unique<A>() };
}

If the armor object is to share the lifetime of the SpaceMarine (and the smart pointer suggests it is) then why is it on the heap at all, not the stack? 如果装甲对象要共享SpaceMarine的生命周期(并且智能指针暗示它是)那么为什么它根本就在堆上,而不是堆栈?

Still, if that's the way you're going then consider defining the class and constructor like this (some parameters and so on left out for brevity):: 尽管如此,如果这是你的方式,那么考虑像这样定义类和构造函数(为了简洁,省略了一些参数等等)::

Declaration: 宣言:

class SpaceMarine
{
public:
    SpaceMarine(Pancerz* armor);
private:
    std::unique_ptr<Pancerz> armor_;
};

Implementation: 执行:

SpaceMarine::SpaceMarine( Panzerc* armor )
: armor_(armor)
{
}

Called like: 被称为:

SpaceMarine obj( new Panzerc() );

or 要么

Panzerc* ptr = new Panzerc();
.   
.
SpaceMarine obj( ptr );

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

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