473,398 Members | 2,404 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,398 software developers and data experts.

object reuse question ..

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

Oct 3 '05 #1
4 1435
ma******@gmail.com wrote:
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
Oct 3 '05 #2
|| 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.

Oct 3 '05 #3
ma******@gmail.com wrote:
Base()
{
if(ptr_transfer == 0)
{
ptr_transfer = new(std::nothrow) transfer();
ptr_transfer->tconect(*this, &Base::test1);
}
}


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
Oct 3 '05 #4

|| 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) {}

Oct 3 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

54
by: Jerry | last post by:
What are the advantages and disadvantages of using Object Oriented PHP vs Java?
34
by: Pmb | last post by:
Hi. I'm new to this group. I'm refreshing/learning C++ and am starting to learn Object Oriented Programming (OOP). In discussing this with people I came up short as to what the benefits of OOP are....
10
by: Henrik Dahl | last post by:
Hello! After I've finished using an instance of the SqlCommand class, should I then invoke Dispose() on the instance. I suppose so, as there is a Dispose method, but what does it actually...
1
by: Jim Hammond | last post by:
The embedded object gets instantiated once when the page is first loaded and then again every time the button is pressed. The user stays on this page after pressing the button because it is not a...
3
by: Simon | last post by:
Hi all, I'm hoping that some of you clever chaps could offer me some advice on code reuse. You see, whenever I make applications, I typically only find very limited
3
by: Marc Thompson | last post by:
Hi there, I have the following: Dim objResponse As WebResponse = objReq.GetResponse Dim objStream As IO.Stream objStream = objResponse.GetResponseStream Then, I read some Bytes from...
12
by: cmay | last post by:
Is there any way to databind the properties of a business object, like an Employee, to fields in a form in asp.net 2.0? cmay
19
by: jacob navia | last post by:
There is an interesting discussion running in Slashdot now, about code reuse. The thema of the discussion is here: < quote > Susan Elliot Sim asks: "In the science fiction novel, 'A Deepness...
46
by: ajba74 | last post by:
Hi fellows, I am reading some books to learn the C programming language, and sometimes I have the feeling that when somebody becomes a C expert, he must learn a more modern and object-oriented...
1
by: vbDavidC | last post by:
Hi, I am fairly new to .net and objects. I learned to create a reader object in method 1, however if I wanted to create multiple select queries in the same module I did not know how to reuse...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.