Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

The best way to use std::auto_ptr...

2 views
Skip to first unread message

ArbolOne

unread,
Oct 28, 2009, 3:34:16 PM10/28/09
to
I have a program that declares an outo-pointer in the header file, and
then instantiate the pointer in the code file.
What are the bi.e.

tester.hpp
~~~~~~~
// My Class
#include "pulldownmenu.hpp"

//STD
#include <memory>

class Tester {
public:
Tester();
~Tester();
private:
PulldownMenu* pdm;

};
tester.cpp
~~~~~~~~
jme::Tester::Tester(){

std::auto_ptr<PulldownMenu> pdm(jPulldownMenu);

}
jme::Tester::~Tester(){}


=====
However, I have seen examples where the pointer is declared and
instantiated in the header file, i.e.
class Tester {
public:
Tester();
virtual ~Tester();
private:
std::auto_ptr<jme::PulldownMenu> pdm(jme::PulldownMenu);


};
=======
What difference does it make? the two ways compile OK!
pls go easy on me, I am new a this auto_ptr thing... :)

Daniel T.

unread,
Nov 1, 2009, 7:12:32 PM11/1/09
to
ArbolOne <arbo...@gmail.com> wrote:

> I have a program that declares an outo-pointer in the header file, and
> then instantiate the pointer in the code file.
> What are the bi.e.
>
> tester.hpp
> ~~~~~~~
> // My Class
> #include "pulldownmenu.hpp"
>
> //STD
> #include <memory>
>
> class Tester {
> public:
> Tester();
> ~Tester();
> private:
> PulldownMenu* pdm;
>
> };
> tester.cpp
> ~~~~~~~~
> jme::Tester::Tester(){
>
> std::auto_ptr<PulldownMenu> pdm(jPulldownMenu);
>
> }
> jme::Tester::~Tester(){}

(In my comments, I'm assuming that jPulldownMenu is of type
PulldownMenu*.)

The above code does not declare an auto pointer in the header file. It
declares a regular pointer in the header file. It also declares and
defines an auto pointer in the cpp file as an object with function
scope. The result is that at the end of the constructer, Tester::pdm
contains an undefined value and whatever jPulldownMenu points to has
been deleted.

That is probably not what you wanted.

> =====
> However, I have seen examples where the pointer is declared and
> instantiated in the header file, i.e.
> class Tester {
> public:
> Tester();
> virtual ~Tester();
> private:
> std::auto_ptr<jme::PulldownMenu> pdm(jme::PulldownMenu);
>
>
> };

The above code doesn't declare a member object called pdm, instead it
declares a member function called pdm that takes a PulldownMenu object
as a parameter and returns an auto_ptr<PulldownMenu>.

That is probably not what you wanted either.

> What difference does it make? the two ways compile OK!

They do completely different things, neither of which is probably want
you want. The appropriate way to make an auto_ptr member variable would
be more like this:

class Tester {
std::auto_ptr<PulldownMenu> pdm;
public:
Tester() : pdm(jPulldowmMenu) { }
};

However there are several problems with even that code. For example, the
first Tester that is destroyed will also destroy whatever jPulldownMenu
points to, which will leave any other tester objects created with
undefined values in their pdm member variables.

I think you are much better off not using auto_ptr at all. If you do use
it, then only use it as an argument to a function or a return value of a
function, and then only if it makes sense in context. 'auto_ptr'
primarily serves as a documentation aid, that has some runtime
enforcement. For example:

auto_ptr<Foo> myFunction();

Tells users of your code that myFunction does not explicitly take care
of the memory returned, that is up to callers to deal with.

void myFunction(auto_ptr<Foo> f);

Tells users that your function will delete the object pointed to by the
parameter and they don't have to worry about it.

0 new messages