简体   繁体   中英

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. But I found a problem I just can't simply solve. Here are some codes that I think are important:

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. Project requirements: I have to make one smart pointer (here as auto_ptr).

.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:

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

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

There is problem with this little thingy, called P2, which should be an auto_ptr to armor. I have armor object P2 declared previously. I have problem "merging" auto_ptr into my constructor. Any ideas?

Also, all advices about improving my code are welcome :)

Matt

Ps. My first post here! :D Go easy on me ^^

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 ;)

Here's the final code for future refrence:

HEADER

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

.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

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. It will transfer ownership to the copy. However, std::auto_ptr is deprecated and should be replaced by 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?

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 );

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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