M
ma740988
Consider the sample source:
# include <iostream>
# include <string>
# include <vector>
using namespace std;
typedef void (*VOIDFUNCPTR)();
class transfer {
public:
transfer() {}
~transfer() {}
template<typename T>
void tconect( T& t, void (T::*f)() )
{}
void do_a_special_thing(){}
// more
};
class Base {
static transfer *ptr_transfer;
public:
Base() : ptr_transfer(0)
{
ptr_transfer = new(std::nothrow) transfer();
ptr_transfer->tconect(*this, &Base::test1);
}
void test1() {}
void test2() { ptr_transfer->do_a_special_thing(); }
};
class Derived1 : public Base {
public:
};
class Derived2 : public Base {
};
int main()
{
Base *ptr_d1 = new Derived1();
Base *ptr_d2 = new Derived2();
}
Now I want Derived1 and Derived2 to use the same ptr_transfer object.
In other words, when Derived1 and Derived2 inherits from Base, they
both need to use the same instance of ptr_transfer.
I suspect the instantiation of the ptr_transfer object doesn't have to
be done in the constructor and I dont want to make class transfer a
singleton. Thanks for the help. Tweak source as you see fit. Thanks
# include <iostream>
# include <string>
# include <vector>
using namespace std;
typedef void (*VOIDFUNCPTR)();
class transfer {
public:
transfer() {}
~transfer() {}
template<typename T>
void tconect( T& t, void (T::*f)() )
{}
void do_a_special_thing(){}
// more
};
class Base {
static transfer *ptr_transfer;
public:
Base() : ptr_transfer(0)
{
ptr_transfer = new(std::nothrow) transfer();
ptr_transfer->tconect(*this, &Base::test1);
}
void test1() {}
void test2() { ptr_transfer->do_a_special_thing(); }
};
class Derived1 : public Base {
public:
};
class Derived2 : public Base {
};
int main()
{
Base *ptr_d1 = new Derived1();
Base *ptr_d2 = new Derived2();
}
Now I want Derived1 and Derived2 to use the same ptr_transfer object.
In other words, when Derived1 and Derived2 inherits from Base, they
both need to use the same instance of ptr_transfer.
I suspect the instantiation of the ptr_transfer object doesn't have to
be done in the constructor and I dont want to make class transfer a
singleton. Thanks for the help. Tweak source as you see fit. Thanks