smart pointer (copy_ptr) as class member

F

Feniks

Hello,
I have some problems with smart pointers. I want to have smart pointer,
copy_ptr in this case in my class like this:


class Entity
{
protected:
.....
copy_ptr<PropertyBag> mProperties; // <- PropertyBag is a class here
.....

public:
Entity(const std::string &Id, const std::string &Type);
~Entity();


The problem is that the compiler returns an error:
Error 1 error C2512: 'copy_ptr<T>' : no appropriate default constructor
available. Should I use another declaration here ? If I try:
copy_ptr<PropertyBag> *mProperties; then compiler doesn't throw any error
but I don't know if it fullfil its role because then I could write
PropertyBag *mProperties and don't have any smart pointer.

Thanks in advance.
Bartek
 
F

Feniks

Or maybe it is a problem with this particular smart pointer that lacks
something ?

template<typename T>
class copy_ptr
{
T * ( *m_clone_fct ) ( T *, bool);
T* m_type;
public:
//Constructor will only clone type that is pass to the constructor
template<typename T_obj>
copy_ptr(T_obj* type):m_type(type),
m_clone_fct(ConstructAndDestruct<T,T_obj, std::allocator<T_obj> > ){}
template<typename T_obj, class AX_TYPE>
copy_ptr(T_obj* type, AX_TYPE):m_type(type),
m_clone_fct(ConstructAndDestruct<T,T_obj, AX_TYPE> ){}
//Destructor
~copy_ptr(){m_type=m_clone_fct(m_type, false);}
//Copy constructor
copy_ptr(const copy_ptr& Src):m_type(Src.m_clone_fct(Src.m_type, true)),
m_clone_fct(Src.m_clone_fct){}

......

Bartek
 
R

Roland Pibinger

I have some problems with smart pointers. I want to have smart pointer,
copy_ptr in this case in my class like this:

class Entity
{
protected:
....
copy_ptr<PropertyBag> mProperties; // <- PropertyBag is a class here

// why not?
PropertyBag mProperties;

//...
};

Best wishes,
Roland Pibinger
 
D

Dan Bloomquist

Roland said:
// why not?
PropertyBag mProperties;

Yes.

At that, there is no constructor for an empty pointer. copy_ptr wants to
hold a pointer to an object. If you had:

copy_ptr<Class> p= new Class;

It works.

PropertyBag would not exist with your code if there were a null pointer
constructor. But if you knew that...

class Entity
{
protected:
PropertyBag* pPB;
public:
Entity(...):pPB( null_ptr ) { ... }
~Entity( ) { if( pPB /*!= null_ptr*/ ) { delete pPB; }
};

Best, Dan.
 
K

Kai-Uwe Bux

Feniks said:
Hello,
I have some problems with smart pointers. I want to have smart pointer,
copy_ptr in this case in my class like this:


class Entity
{
protected:
....
copy_ptr<PropertyBag> mProperties; // <- PropertyBag is a class here
....

public:
Entity(const std::string &Id, const std::string &Type);
~Entity();


The problem is that the compiler returns an error:
Error 1 error C2512: 'copy_ptr<T>' : no appropriate default constructor
available. Should I use another declaration here ?
[snip]

The copy_ptr you are using seems to require a valid pointee (i.e.,
deliberately does not support 0-pointers). In that case, you need to create
a pointee, like

copy_ptr< PropertyBag > mProperties( new PropertyBag );

However, the whole point of using a copy_ptr instead of an object of type
PropertyBag is that the copy_ptr could have a poinee of type derived from
PropertyBag. If you want to make use of that, you should probably postpone
the declaration of mProperties until you know which type to use in the new
expression. If you don't want to make use of polymorphism, a simple member
object of type PropertyBag should do just fine.


Best

Kai-Uwe Bux
 
F

Feniks

The copy_ptr you are using seems to require a valid pointee (i.e.,
deliberately does not support 0-pointers). In that case, you need to
create
a pointee, like

copy_ptr< PropertyBag > mProperties( new PropertyBag );

However, the whole point of using a copy_ptr instead of an object of type
PropertyBag is that the copy_ptr could have a poinee of type derived from
PropertyBag. If you want to make use of that, you should probably postpone
the declaration of mProperties until you know which type to use in the new
expression. If you don't want to make use of polymorphism, a simple member
object of type PropertyBag should do just fine.


Best

Kai-Uwe Bux


Yeap, I've used smart pointer in the stl vector to hold derived classes as
well. The main reason from changing *PropertyBag to its smart pointer
version was that when I tried to push back this class into the vector I got
memory violation error. Finally I've changed that smart pointer to
shared_ptr from boost library and it's working fine...for now :).

Cheers
Bartek
 

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
474,431
Messages
2,571,679
Members
48,796
Latest member
Greg L.

Latest Threads

Top