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

prohibit creation of object copies

Hello,

what is the best way to prevent the user of a class from making copies of
objects of this class? Making the copy constructor private is a solution but I'm
looking for a better one.

Thanks,
Wolfgang

Jul 19 '05 #1
5 2477

"Wolfgang Jeltsch" <je*****@tu-cottbus.de> wrote in message news:3F**************@tu-cottbus.de...
Hello,

what is the best way to prevent the user of a class from making copies of
objects of this class? Making the copy constructor private is a solution but I'm
looking for a better one.


We make the copy constructor (and also frequently the assignment op) private
and we don't implement it. Not only does this give compile errors when you
try to make external copies, but (at least on our systems) link errors if you
try to make copies inside the class.
Jul 19 '05 #2
Wolfgang Jeltsch wrote:
Hello,

what is the best way to prevent the user of a class from making copies
of objects of this class? Making the copy constructor private is a
solution but I'm looking for a better one.

Thanks,
Wolfgang


Search the web for "Singleton Design Pattern".
Also search this newsgroup and news:comp.object.

--
Thomas Matthews

C++ newsgroup welcome message:
http://www.slack.net/~shiva/welcome.txt
C++ Faq: http://www.parashift.com/c++-faq-lite
C Faq: http://www.eskimo.com/~scs/c-faq/top.html
alt.comp.lang.learn.c-c++ faq:
http://www.raos.demon.uk/acllc-c++/faq.html
Other sites:
http://www.josuttis.com -- C++ STL Library book

Jul 19 '05 #3
Wolfgang Jeltsch wrote:
Hello,

what is the best way to prevent the user of a class from making copies
of objects of this class? Making the copy constructor private is a
solution but I'm looking for a better one.


Better in which regard? It's quite simple, does the job and is the usual
technique, so what could be done 'better'?

Jul 19 '05 #4
Wolfgang Jeltsch wrote:
Hello,

what is the best way to prevent the user of a class from making copies of
objects of this class? Making the copy constructor private is a solution
but I'm looking for a better one.

Thanks,
Wolfgang


I find this more elegant

class NonCopyable
{
protected:
NonCopyable() {}

private:
NonCopyable( const NonCopyable& );
NonCopyable& operator=( const NonCopyable& );
};

class Derived : public NonCopyable {}

---
Filipe Sousa
Jul 19 '05 #5
On Fri, 05 Sep 2003 13:09:27 +0100, Filipe Sousa
<fi*************@alunos.ipb.pt> wrote:
Wolfgang Jeltsch wrote:
Hello,

what is the best way to prevent the user of a class from making copies of
objects of this class? Making the copy constructor private is a solution
but I'm looking for a better one.

Thanks,
Wolfgang


I find this more elegant

class NonCopyable
{
protected:
NonCopyable() {}

private:
NonCopyable( const NonCopyable& );
NonCopyable& operator=( const NonCopyable& );
};

class Derived : public NonCopyable {}


Or, similarly,

#include <boost/utility.hpp>

class Derived: boost::noncopyable {};

Tom
Jul 19 '05 #6

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

Similar topics

4
by: Zork | last post by:
Hi, I am trying to stop object creation (in this case ill use a ball as the object) via use of exceptions. In essence, if the ball does not have an owner, I do not want the ball object created....
6
by: | last post by:
I have a class with overloading operator new. (Because, if an identical object exists, return a pointer to existed object instead of a new pointer) It has no sense (it is dangerous) to allocate an...
1
by: jsnX | last post by:
hi, i would like to make some code that operates on sets of points - does convex hulls and other operations. but it looks like i will need a lot of pointless object copies: ...
8
by: Anthony Munter | last post by:
I have a web application with impersonate=”true” in Web.config and on my own logon page I allow the user to either - specify a userid/password for the app to impersonate when calling legacy...
3
by: Nick Dreyer | last post by:
I was quite surprised to notice that Sub New() gets called twice, once at declaration time and once at creation time. I can't figure out why it would be called at declaration if there is no class...
0
by: sonu | last post by:
I have following client side code which i have used in my asp.net project SummaryFeatured Resources from the IBM Business Values Solution Center WHITEPAPER : CRM Done Right Improve the...
4
by: Boni | last post by:
Dear all, can I prohibit that my class will be passed by value (i.e created a copy on stack when function is called)? I want to prohibit passing my class by value. I thougth that declare a default...
9
by: bonk | last post by:
Does anyone have a simple example on how to prohibit that any thread other than the current thread modifies a certain object (a collection) while we are in a certain section of the code? In other...
3
by: ALEXURC | last post by:
I'm looking for a simple method to delete a folder after 72 "Business hours" (saturday/sunday doesnt count) since its creation. Note that This is on a linux system and I realize that it will be the...
1
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: 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...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
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: 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: 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
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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...

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.