initializing std::auto_ptr variable in class constructor - how to dow this?

X

XP

I was wondering if this is possible. As I currently use something like this:

class Ta {
public: Type * pType;
Ta();
~Ta();
}

// Constructor - initialize pType
Ta::Ta() {
pType = new Type();
}

// destroy pType
Ta::~Ta() {
delete pType;
}

So... how would you do it with std::auto_ptr so that it doesn't go out of
scope? Or maybe I should go with boost::shared_ptr with one additional
reference that won't go out of scope. I could place std::auto_ptr<Type>
pType(new Type()); but that would of course go out of scope as soon as
constructor has finished. Any way to force it to stay defined until
destructor?

The point is to avoid new and delete and use auto_ptr or something similar
in this example class.
 
M

Marcel Müller

XP said:
I was wondering if this is possible. As I currently use something like this:

class Ta {
public: Type * pType;
Ta();
~Ta();
}

// Constructor - initialize pType
Ta::Ta() {
pType = new Type();
}

// destroy pType
Ta::~Ta() {
delete pType;
}

So... how would you do it with std::auto_ptr so that it doesn't go out of
scope?

class Ta
{ std::auto_ptr<Type> pType;
public:
Ta() : pType(new Type()) {}
};

But note that this will make your class non-copyable.

> Or maybe I should go with boost::shared_ptr with one additional
reference that won't go out of scope.

? - what is the meaning of 'additional reference'?

Furthermore the use of shared_ptr, as the name suggests, will result in
copies of Ta that share the same instance of Type (unless you force
something else by an explicit implementation of copy constructor and
assignment operator). This might not be intended.
I could place std::auto_ptr<Type>
pType(new Type()); but that would of course go out of scope as soon as
constructor has finished. Any way to force it to stay defined until
destructor?

Yes, see above.

It will not go out of scope until the auto_ptr goes out of scope. And
this is exactly after the destructor of Ta.


Marcel
 
M

mzdude

I was wondering if this is possible. As I currently use something like this:

class Ta {
    public: Type * pType;
            Ta();
            ~Ta();
    }

public: std::auto_ptr said:
// Constructor - initialize pType
Ta::Ta() {
pType = new Type();

}
Ta::Ta() : pType(new Type() ) {}
Ta::~Ta() {}

pType is valid until dtor runs (or auto_ptr releases control)

Be sure to implement or disable the copy ctor and
assignment operator.
// destroy pType
Ta::~Ta() {
delete pType;

}

Leaving your data member public is asking for trouble.
 
Ö

Öö Tiib

Be sure to implement or disable the copy ctor and
assignment operator.

XP should do that with his original example anyway. With scoped_ptr<>
it would be easier than with auto_ptr<>. scoped_ptr<> member
automatically disables default copy constructor and assignment
operator so one has to implement such if he needs them.
 
X

XP

i was hoping that there is a bit more elegant solution with auto_ptr (or
scoped_ptr) but this looks like even more mess. Thanks for your answers
though.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,226
Members
46,815
Latest member
treekmostly22

Latest Threads

Top