By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,345 Members | 1,706 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,345 IT Pros & Developers. It's quick & easy.

smart pointer (copy_ptr) as class member

P: n/a
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<PropertyBagmProperties; // <- 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
Nov 11 '06 #1
Share this Question
Share on Google+
5 Replies


P: n/a
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
Nov 11 '06 #2

P: n/a
On Sat, 11 Nov 2006 13:41:39 +0100, "Feniks" wrote:
>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<PropertyBagmProperties; // <- PropertyBag is a class here
// why not?
PropertyBag mProperties;

//...
};

Best wishes,
Roland Pibinger
Nov 11 '06 #3

P: n/a


Roland Pibinger wrote:
On Sat, 11 Nov 2006 13:41:39 +0100, "Feniks" wrote:
>>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<PropertyBagmProperties; // <- PropertyBag is a class here


// 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<Classp= 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.

Nov 11 '06 #4

P: n/a
Feniks wrote:
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<PropertyBagmProperties; // <- 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
Nov 11 '06 #5

P: n/a
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
Nov 11 '06 #6

This discussion thread is closed

Replies have been disabled for this discussion.