473,386 Members | 1,795 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,386 software developers and data experts.

Need a cloning smart pointer

Hi,

I need something like this:

class X {
private:
struct Impl_ {
virtual ~Impl_() {}
virtual Impl_* clone() const = 0;
};
Impl_* impl_;
public:
// ...
};

'X' should have value semantics, so '*impl_'
must be cloned when 'X' is copied. I know
how to do this and I have don it a couple
of times. And that is bothering me. I would
rather have a smart pointer that supports
cloning semantics and use this whenever I
run into this pattern.
Is there a smart pointer out there doing
this or do I have to roll my own?
Note: Ideally, it would come in just one
header file. I do not want to add a whole
new library to get s cloning smart pointer.

I have full control over 'X' and 'X::Impl',
so I could actually make them fit some ready-
made smart pointer if I find one.

Schobi

--
Sp******@gmx.de is never read
I'm Schobi at suespammers dot org

"Sometimes compilers are so much more reasonable than people."
Scott Meyers
Nov 17 '05 #1
2 1738
Check out the boost libraries, they've got a boost::shared_ptr and
boost::scoped_ptr which are both smart pointers. The shared_ptr includes
reference counting and all that nice stuff. The boost libs in general are
extremely good for a lot of stuff.

Steve
"Hendrik Schober" <Sp******@gmx.de> wrote in message
news:ez**************@TK2MSFTNGP12.phx.gbl...
Hi,

I need something like this:

class X {
private:
struct Impl_ {
virtual ~Impl_() {}
virtual Impl_* clone() const = 0;
};
Impl_* impl_;
public:
// ...
};

'X' should have value semantics, so '*impl_'
must be cloned when 'X' is copied. I know
how to do this and I have don it a couple
of times. And that is bothering me. I would
rather have a smart pointer that supports
cloning semantics and use this whenever I
run into this pattern.
Is there a smart pointer out there doing
this or do I have to roll my own?
Note: Ideally, it would come in just one
header file. I do not want to add a whole
new library to get s cloning smart pointer.

I have full control over 'X' and 'X::Impl',
so I could actually make them fit some ready-
made smart pointer if I find one.

Schobi

--
Sp******@gmx.de is never read
I'm Schobi at suespammers dot org

"Sometimes compilers are so much more reasonable than people."
Scott Meyers

Nov 17 '05 #2
Steve McLellan <sj*@fixerlabs.com.NOSPAM> wrote:
Check out the boost libraries, they've got a boost::shared_ptr and
boost::scoped_ptr which are both smart pointers. The shared_ptr includes
reference counting and all that nice stuff. The boost libs in general are
extremely good for a lot of stuff.
That's why we use them.
However, they do not have a cloning
smart pointer.
Steve
Schobi
[...]


Schobi

--
Sp******@gmx.de is never read
I'm Schobi at suespammers dot org

"Sometimes compilers are so much more reasonable than people."
Scott Meyers
Nov 17 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Vijai Kalyan | last post by:
I have been thinking about this and it may have already been thrashed out and hung out to dry as a topic of no more interest but here goes. I found when implementing a smart pointer that the...
8
by: Axter | last post by:
I normally use a program call Doxygen to document my source code.(http://www.stack.nl/~dimitri/doxygen) This method works great for small and medium size projects, and you can get good...
6
by: zl2k | last post by:
hi, When I considered about preventing memory leaking, the method came up to my mind is using boost smart pointer if possible (use stl::vector instead of type, use smart pointer whenever declare...
14
by: Ian | last post by:
I am looking at porting code from a C++ application to C#. This requires implementing data sharing functionality similar to what is provided by a smart pointer in C++. I have only recently begun...
33
by: Ney André de Mello Zunino | last post by:
Hello. I have written a simple reference-counting smart pointer class template called RefCountPtr<T>. It works in conjunction with another class, ReferenceCountable, which is responsible for the...
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...
5
by: Soumen | last post by:
Hi, I've requirement to observe a raw pointer (i.e. I should be able to query if the pointer I'm using is still valid or not but when the observer goes out of scope, the resource -- memory --...
13
by: Phil Bouchard | last post by:
I am currently writting a smart pointer which is reasonnably stable and I decided supporting allocators for completion because of its increase in efficiency when the same pool used by containers is...
50
by: Juha Nieminen | last post by:
I asked a long time ago in this group how to make a smart pointer which works with incomplete types. I got this answer (only relevant parts included): ...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
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,...
0
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...

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.