473,320 Members | 1,839 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

smart pointer (copy_ptr) as class member

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
5 1920
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
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


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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Matthias Kaeppler | last post by:
Hi, I was wondering, since STL containers are based around copying, whether it's a good idea to use reference counted smart pointers, such as boost::shared_ptr in STL containers. I can't store...
3
by: yinglcs | last post by:
In Effective STL item 8, it said 'Never create cointainers of auto_ptrs'. But in the Boost shared_ptr_example.cpp example, it creates a stl vector of smart pointer. Why it is okay in this case?...
3
by: Nindi73 | last post by:
Hi, I am in need of a deep copy smart pointer (Boost doesn't provide one) which doesnt require the contained types to have a virtual copy constructor. I wrote a smart pointer class that I think...
11
by: Nindi73 | last post by:
A few days a ago I posted my code for a deep copy pointer which doesn't require the pointee object to have a virtual copy constructor. I need help with checking that it was exception safe and...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.