-
Creating an assignment operator and copy constructor
Say class B is a singleton
class B
{ private:
static B* pInstance;
public:
static B* Instance();
.
.
};
Also say class A holds a reference to B.
class A
{ private:
B& refr;
.
.
};
How do I construct the assignment operator for class A.
Also how would the copy constructor be coded, for example, as shown below:
A::A(const A& other) : refr(other.refr)
{ .
.
}
-
Singleton classes aren't meant to be contained as members of other classes. The whole idea tis to provide a global access function that returns the singleton object.
However, copying B objects is a trivial operation. Simply use the default assignment operator and copy constructor of B (assuming they're not private, which they ought to be!). The class doesn't contain any nonstatic data members anyway, so copying it merely copies a single random byte that is never accessed or used.
The real question is why you want to do this. To use A you never even need an instance thereof: all you do is call the Instance() static function like this:
p=A::Instance();
So obviously, you can't define a reference to A when there are no instances of A!
Danny Kalev
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
Forum Rules
|
Top DevX Stories
Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL
|
Bookmarks