473,938 Members | 6,648 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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<Proper tyBagmPropertie s; // <- 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<Proper tyBag*mProperti es; 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 1943
Or maybe it is a problem with this particular smart pointer that lacks
something ?

template<typena me 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<typena me T_obj>
copy_ptr(T_obj* type):m_type(ty pe),
m_clone_fct(Con structAndDestru ct<T,T_obj, std::allocator< T_obj ){}
template<typena me T_obj, class AX_TYPE>
copy_ptr(T_obj* type, AX_TYPE):m_type (type),
m_clone_fct(Con structAndDestru ct<T,T_obj, AX_TYPE){}
//Destructor
~copy_ptr(){m_t ype=m_clone_fct (m_type, false);}
//Copy constructor
copy_ptr(const copy_ptr& Src):m_type(Src .m_clone_fct(Sr c.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<Proper tyBagmPropertie s; // <- 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<Prop ertyBagmPropert ies; // <- 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<Proper tyBagmPropertie s; // <- 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
2582
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 the objects directly in a container, because they must not be duplicated, so I have to use pointers. I'm just not certain about using raw pointers or some kind of smart pointer. Regards,
3
8210
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? Here is part the code: struct Foo {
3
2135
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 meets these requirements, but after reading the chapter on exceptions in 'Exceptional C++':Sutter, I am not sure if its is really Exception safe or Exception Neutral. I suppose putting the theory in that chapter into practice isn't trivial.
11
3450
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 exception neutral/ I got a reply from Bux with his code for a smart pointer with far fewer lines of code and more cleaner design, not over engineered like mine. ...
0
10134
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9963
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
11524
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
11106
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
11288
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10657
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
8218
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
7381
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
2
4446
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.