Need of Overloading Operators

Hi, I am having following doubts

1. If = operator is already overloaded for a class why do we need to explicitly write code to overload = operator .


2. How to design a Singleton Class such that each thread of the code can instantiate only one object out of it.
1. C++ compilers provide a bit by bit copy implementation of operator= and the copy constructor. This is not desirable if you need to, for example, duplicate handles or data in dynamic memory like dynamically-allocated strings.

2. The first thought that comes to my mind: Use Thread Local Storage (TLS). I am unsure if this works in every OS, but I know it is available for Windows.
webjose wrote:
C++ compilers provide a bit by bit copy implementation of operator= and the copy constructor.

The default copy constructor and assignment operator uses copy constructors for copying object member types.
Yes, and they use the operator= too of member fields. I did not want to make a mess for the OP here. My explanation was kept simple intentionally assuming simple data types, like pointers or Windows handles.
Alright, I had no doubt you didn't know that, just wanted to make sure all necessary information is here for (future) readers.
If a class is a singleton, there can be only one instance of it. You can't have one singleton per thread.

But if you implement your thread as a class, you could add a member of your "singleton" class to the thread.

Could be possible that the strict definition of a singleton doesn't allow to include "per thread" in it, but the idea is well understood, at least by me, and I guess by some others.

For example, in C#, singletons are stored in static field members, and MS .Net has the ThreadStaticAttribute attribute (http://msdn.microsoft.com/en-us/library/system.threadstaticattribute.aspx ) that modifies any static field in the way the OP wants it. It is therefore not an uncommon request, IMO.
Topic archived. No new replies allowed.