I'm having trouble understanding what you want but I'll try...
I create Myclass foo ("foo",10) and then Myclass foo2(foo). I want
all ref to be equal to 2. Here only the ref of foo2 is equal to 2.
The ref of foo is equal to 1. I think I need a pointer, no?
This can be accomplished with a static variable:
class Myclass
{
public:
Myclass(const char * foo, int foo2) : foo(foo), foo2(foo2)
{
ref += 1;
if (1 == ref) {} // First ref, do something?
}
Myclass(const Myclass& rhs) :foo(rhs.foo),foo2(rhs.foo2)
{
ref += 1;
}
~Myclass() // Decrement on delete
{
ref -= 1;
if (0 == ref) {} // Last reference. Do something?
}
const char * foo;
int foo2;
static int ref;
};
int Myclass::ref = 0; // Initialize to 0
Then....
Myclass foo("foo",10); // ref becomes 1
Myclass foo2(foo); // ref becomes 2
Myclass *foo3 = new Myclass(foo); // ref becomes 3
delete foo3; // ref becomes 2