object reuse question ..

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
 
D

David White

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)

You can't initialize a static member like that.
{
ptr_transfer = new(std::nothrow) transfer();
ptr_transfer->tconect(*this, &Base::test1);
}

You can make this:
Base()
{
if(ptr_transfer == 0)
{
ptr_transfer = new(std::nothrow) transfer();
}
ptr_transfer->tconect(*this, &Base::test1);
}

However, the call to tconect looks a bit of a worry to me. I've put it
outside the test for null because I'm assuming that both your Derived[12]
objects are equivalent, so you wouldn't want only the first one created to
connect. However, will the transfer object operate correctly if tconect is
called multiple times?
void test1() {}
void test2() { ptr_transfer->do_a_special_thing(); }
};

class Derived1 : public Base {
public:
};

class Derived2 : public Base {
};

Something I didn't address above is when you delete the transfer object. I
prefer the version below, in which you don't have to worry about that.
class Base {
static transfer transfer_;
public:
Base()
{
transfer_.tconect(*this, &Base::test1);
}
void test1() {}
void test2() { transfer_.do_a_special_thing(); }
};


// In .cpp, .cxx etc. file
transfer Base::transfer_;
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.

Is this true of all instances of Base/Derived[12], or just the two above?
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

DW
 
M

ma740988

|| Base()
|| {
|| if(ptr_transfer == 0)
|| {
|| ptr_transfer = new(std::nothrow) transfer();
|| ptr_transfer->tconect(*this, &Base::test1);
|| }
|| }

David, I moved tconnect but excellent point.

|| Is this true of all instances of Base/Derived[12],
|| or just the two above?

All instances.

Thanks for the tip.
 
D

David White

David, I moved tconnect but excellent point.

Okay, if that's where it should be, but I'm puzzled at your passing *this to
the function. You are implying that the first object created is a special
case, since only that one calls tconect. My instincts are telling me that
maybe something is not right about this. And if you are using the pointer
version, I'm curious as to how you know when it's okay to delete the
transfer object. I also wonder if it matters in what order the Derived[12]
objects are destroyed.

DW
 
M

ma740988

|| You are implying that the first object created is a special case,
since only that one calls tconect.
|| My instincts are telling me that maybe something is not right about
this.

The more I think about it, I'm better off passing in the transfer
object to the derived classes.

Derived1(transfer& t) {}
Derived2(transfer& t) {}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top