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)
{ .
.
}
[535 byte] By [
ray] at [2007-11-11 8:25:52]

# 1 Re: Creating an assignment operator and copy constructor
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 at 2007-11-11 21:01:22 >
